我有一个像这样的头文件:
#ifndef my_data_h
#define my_data_h
struct Student{
int GPA;
int coursesCount;
float tuitionFees;
};
struct employee{
float salary;
int yearOfService;
int salaryLevel;
};
struct person{
char firstName[11];
char familyName[21];
char telephone[11];
int isStudent;
struct Student student;
struct employee employee;
};
#endif
我还有学生的 Student.h 和 Student.c 文件。
学生.h
#include <stdio.h>
#include "data.h"
void getStudentData(struct Student);
学生.c
#include "student.h"
#include "data.h"
void getStudentData(struct Student currentStudent){
}
现在我有一个带有 main 的 .c 文件,我在其中调用如下内容: getStudentData(myperson.student);
我包含在这个 c 文件中的所有 header 都有 main.h 。
#include <stdio.h>
#include "student.h"
#include "employee.h"
#include "data.h"
但是在student.c文件中出现错误,getStudentData的类型冲突
如何解决?
我还需要在student.h中定义学生结构吗?
类似于:struct Student Student;
最佳答案
在main.c中
#include "header.h" // where structs are declared
#include "student.h"
void getStudentData(struct Student currentStudent){
}
或者在“student.h”中
#include <stdio.h>
#include "header.h" // where structs are declared
void getStudentData(struct Student);
关于c 中结构的冲突类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33188090/