如何在c

时间:2019-01-19 19:26:31

标签: c

老实说,我不习惯使用c语言,也许在解决问题时我错了。

我有3个文件,我需要将数据从一个传递到另一个

//main.c

#include <stdio.h>
#include "process.h"
#include "readFile.h"    

int main() {
    int numIn=2, numOut=1;

    struct data *allData=readData(numIn, numOut);

    process(allData, numIn, numOut);

    return 0;
}

// readFile.h

#include <stdio.h>
#include <stdlib.h>

struct data {
    double *in;
    double *out;
};


struct data * readData(int numIn, int numOut) {
   //here I initialize and fill an "allData" array of struct data

   return allData;
}

//process.h

#include <stdio.h>
#include <stdlib.h>
#include "readFile.h"

int process(struct data * allData, int numIn, int numOut) {

    return 0;
}

如果我删除“ process.h”并尝试在主窗口中打印“ allData”,则正确的数据将正确打印,但是当我尝试在“ process.h”中处理数据时,出现此编译错误:

In file included from C:\...\main.c:4:0:
C:\...\readFile.h:11:8: error: redefinition of 'struct data'
 struct data
        ^
In file included from C:\...\process.h:11:0,
                 from C:\...\main.c:2:
C:\...\readFile.h:11:8: note: originally defined here
 struct data
        ^
In file included from C:\...\main.c:4:0:
C:\...\readFile.h:24:15: error: conflicting types for 'readData'
 struct data * readData(int numIn, int numOut)
               ^
In file included from C:\...\process.h:11:0,
                 from C:\...\main.c:2:
C:\...\readFile.h:24:15: note: previous definition of 'readData' was here
 struct data * readData(int numIn, int numOut)
               ^

1 个答案:

答案 0 :(得分:0)

请勿在.h文件中放置任何代码

.h文件用于数据声明,外部变量声明和函数声明。

.c文件是具有变量和函数定义的正确位置。

将所有代码从.h文件移动到.c文件

还添加.h文件防护。这只是定义。如果已经定义了该定义-则意味着该文件已经包含在内,应跳过其内容

#ifdef MYGUARD_H
#define MYGUARD_H

/* .h file content

#endif