我剛開始使用C語言編寫模塊化編程。我想我對包含內容做了一些錯誤,因爲我收到了很多conflicting types for 'functionName'
和previous declaration of 'functionName' was here
錯誤。我確實把保衛人員放在了位置上。在C中解釋模塊化編程的清晰教程?
您是否知道一個清楚的教程,解釋C語言中的模塊化編程,尤其是這些包含文件如何工作?
更新:我試圖孤立我的問題。根據要求,這是一些代碼。
更新2:更新代碼如下。錯誤也已更新。
/*
* main.c
*/
#include <stdio.h>
#include "aStruct.h"
int main() {
aStruct asTest = createStruct();
return 0;
}
/*
* aStruct.h
*/
#ifndef ASTRUCT_H_
#define ASTRUCT_H_
struct aStruct {
int value1;
int value2;
struct smallerStruct ssTest;
};
typedef struct aStruct aStruct;
aStruct createStruct();
#endif /* ASTRUCT_H_ */
/*
* smallerStruct.h
*/
#ifndef SMALLERSTRUCT_H_
#define SMALLERSTRUCT_H_
struct smallerStruct {
int value3;
};
typedef struct smallerStruct smallerStruct;
smallerStruct createSmallerStruct();
#endif /* SMALLERSTRUCT_H_ */
/*
* aStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
#include "aStruct.h"
aStruct createStruct() {
aStruct asOutput;
printf("This makes sure that this code depends on stdio.h, just to make sure I know where the inclusion directive should go (main.c or aStruct.c).\n");
asOutput.value1 = 5;
asOutput.value2 = 5;
asOutput.ssTest = createSmallerStruct();
return asOutput;
}
/*
* smallerStruct.c
*/
#include <stdio.h>
#include "smallerStruct.h"
smallerStruct createSmallerStruct() {
smallerStruct ssOutput;
ssOutput.value3 = 41;
return ssOutput;
}
這會產生以下錯誤消息:
At aStruct.h:10
- field 'ssTest' has incomplete type
At main.c:8
- unused variable `asTest' (this one makes sense)
你可以發佈一個代碼給你帶來麻煩嗎? – FrustratedWithFormsDesigner 2010-02-19 14:26:51
我按要求添加了一個樣本。 – Pieter 2010-02-19 16:29:44
你需要在astruct.h中#include「smallerStruct.h」 – 2010-02-20 10:54:24