【问题标题】:Type of formal parameter 1 is incomplete error形参类型 1 不完整错误
【发布时间】:2018-04-20 09:22:34
【问题描述】:

我正在尝试编写一个打印学生信息的程序 但是 Code::Blocks 说:

错误:形式参数 1 的类型不完整。 错误:“displayStudentInformation”的类型冲突 这是代码。

#include <stdio.h>

void displayStudentInformation(struct student stu);

struct student{
  int id;
  char *firstName;
  char *lastName;
  float gpa;
};

int main()
{
  struct student stu1;
  stu1.id = 101;
  stu1.firstName = "Ali";
  stu1.lastName = "Alavi";
  stu1.gpa = 18;

  displayStudentInformation(stu1);

  return 0;
}

void displayStudentInformation(struct student stu)
{
  printf("Student information :\n");
  printf("ID : %d",stu.id);
  printf("First Name : %s",stu.firstName);
  printf("Last Name :%s",stu.lastName);
  printf("GPA : %.2f",stu.gpa);
  printf("\n");
}

【问题讨论】:

  • 交换structdisplayStudentInformation的声明
  • 意思是将struct student的声明放在displayStudentInformation()函数的声明之前。
  • @Amin Ataee 只需将程序编译为 C++ 程序即可。;)
  • @VladfromMoscow 这不是 C 和 C++ 语法之间的根本区别,但我不喜欢 C++。
  • @AminAtaee 不过该程序将编译为 C++ 程序。:)

标签: c++ c struct scope


【解决方案1】:

在函数原型之前定义结构体:

struct student {
  int id;
  char *firstName;
  char *lastName;
  float gpa;
};

void displayStudentInformation(struct student stu);

现在结构是已知的,可以在函数原型中使用。

请注意,您可以 typedef 结构体,以便不必在每次要声明 student 类型的变量时都编写 struct student

typedef struct student {
  int id;
  char *firstName;
  char *lastName;
  float gpa;
} student;

void displayStudentInformation(student stu);

由于某种原因,您的问题也被标记为“c++”(?)我必须在这里提到,在 C++ 中,您不需要 typedef 它。即使没有typedef,您也可以只使用student 而不是struct student

如果您打算使用 C++ 而不是 C,那么我还要提一下,对于字符串,建议使用 std::string 而不是 char*。请注意,与 C 相比,C++ 是一种非常不同的语言。如果您当前的目标或任务是学习 C,那么不要只使用 C++ 编译器编译您的 C 代码。请使用 C 编译器。如果您使用 C++ 编译器,您可以(并且可能会)得到有效 C++ 但无效 C 的代码。

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2011-04-29
    • 1970-01-01
    相关资源
    最近更新 更多