![](/img/trans.png)
[英]Typedef redefinition with different types ('struct TGAME' vs 'struct TGAME')
[英]Redefinition; different basic types (typedef struct)
當在不同的文件中定義結構時,我在嘗試使結構正常工作時遇到了一些麻煩。 據我所知,錯誤告訴我結構被定義了兩個不同的時間。 我相信也許我可能需要在某處使用extern? 我嘗試過試驗並在Google上尋求幫助,但無濟於事。
任何幫助都將非常感謝,謝謝。 我的所有四個文件都在下面。
文件:Foo.h
typedef struct
{
int number;
} my_struct; // Redefinition; different basic types
文件:Foo.c
#include "Foo.h"
#include "Bar.h"
#include <stdio.h>
my_struct test;
int main(void)
{
test.number = 0;
DoSomething(&test);
printf("Number is: ", &test.number);
}
文件:Bar.h
#include "Foo.h"
void DoSomething(my_struct *number);
文件:Bar.c
#include "Bar.h"
void DoSomething(my_struct *number)
{
number->number = 10;
}
問題是你在Foo.h
中有Bar.h
並且Foo.h
和Bar.h
都包含在main.cpp
,這導致在翻譯單元中兩次獲得my_struct
定義。 在struct定義文件周圍有一個ifdef
指令。 試試這個 -
#ifndef FOO_H
#define FOO_H
typedef struct
{
int number;
} my_struct;
#endif
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.