c 中结构的冲突类型

标签 c header-files

我有一个像这样的头文件:

#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/

相关文章:

c - 回复 : Struct Array with Varying Size Structs--Clarification

c++ - 为什么缓冲区末尾和保存的帧指针之间有 8 个字节?

c - header 相互使用结构和枚举?

c - 在自定义函数中再次包含相同的头文件,哪些已经包含在主程序中?

c - 在 MSVS 2012 Express 中构建 OpenCl 示例时出现问题。没有找到头文件,但找到路径等...看起来不错

c - 分配 struct = struct 时出现段错误

c - 访问结构体中的成员?

c++ - C 函数从 iOS 设备设置中查找 iOS 用户 12/24 小时偏好

c++ - 在 ';' 之前缺少 'template<'

c++ - 如何仅在本地 header 上运行预处理器?