c中结构的冲突类型

时间:2015-10-17 15:07:18

标签: 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文件。

student.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文件中包含的所有标题都有main。

 #include <stdio.h>
 #include "student.h"
 #include "employee.h"
 #include "data.h"

但是我在student.c文件中遇到错误 getStudentData的冲突类型

如何解决?

我还需要在student.h中定义学生结构吗?

类似于: struct Student student;

2 个答案:

答案 0 :(得分:1)

在main.c中

#include "header.h" // where structs are declared
#include "student.h"
void getStudentData(struct Student currentStudent){

}

或在&#34; student.h&#34;

#include <stdio.h>
#include "header.h" // where structs are declared
void getStudentData(struct Student);

答案 1 :(得分:0)

问题的最可能原因是您包含头文件(包含结构定义的文件)多次,即使您没有意识到。要避免这种情况,请在标头文件中使用inclusion guard

#ifndef FILE_H
#define FILE_H

// here goes the content of your header

#endif
相关问题