简体   繁体   English

重新定义;不同的基本类型(typedef struct)

[英]Redefinition; different basic types (typedef struct)

I'm having a bit of trouble trying to get structs to work properly when they are defined in different files. 当在不同的文件中定义结构时,我在尝试使结构正常工作时遇到了一些麻烦。 From as far as I can tell, the error is telling me that the struct is being defined two different times. 据我所知,错误告诉我结构被定义了两个不同的时间。 I believe that perhaps I may need to use extern somewhere? 我相信也许我可能需要在某处使用extern? I've tried experimenting and looking for help on Google, but to no avail. 我尝试过试验并在Google上寻求帮助,但无济于事。

Any help at all would be most appreciated, thank you. 任何帮助都将非常感谢,谢谢。 All four of my files are below. 我的所有四个文件都在下面。

FILE: Foo.h 文件:Foo.h

typedef struct
{
    int number;
} my_struct;    // Redefinition; different basic types

FILE: Foo.c 文件: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);
}

FILE: Bar.h 文件:Bar.h

#include "Foo.h"

void DoSomething(my_struct *number);

FILE: Bar.c 文件:Bar.c

#include "Bar.h"

void DoSomething(my_struct *number)
{
    number->number = 10;
}

The problem is you have Foo.h in Bar.h . 问题是你在Foo.h中有Bar.h And both Foo.h and Bar.h are being included in main.cpp , which results getting the my_struct definition twice in the translation unit. 并且Foo.hBar.h都包含在main.cpp ,这导致在翻译单元中两次获得my_struct定义。 Have a ifdef directive around struct definition file. 在struct定义文件周围有一个ifdef指令。 Try this - 试试这个 -

#ifndef FOO_H
#define FOO_H

  typedef struct
  {
      int number;
  } my_struct;    

#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typedef 用不同类型重新定义('struct TGAME' vs 'struct TGAME') - Typedef redefinition with different types ('struct TGAME' vs 'struct TGAME') typedef 重定义不同类型(&#39;struct word&#39; vs &#39;struct word&#39;)}; - typedef redefinition with different types ('struct word' vs 'struct word') }; 错误C2371:'functionname'重定义:不同的基本类型 - error C2371: 'functionname' redefinition: different basic types 错误C2371,重新定义; 不同的基本类型 - error C2371, redefinition; different basic types 错误C2371:重新定义; 不同的基本类型 - 为什么? - Error C2371: redefinition; different basic types - why? C2371:&#39;mxArray&#39;:重新定义; 不同的基本类型 - C2371: 'mxArray' : redefinition; different basic types 将程序分为多个文件-出现错误“重新定义; 不同的基本类型” - Breaking program into multiple files - getting error “redefinition; different basic types” 错误:不同类型的typedef重新定义(“ unsigned short”与“ __darwin_size_t”(又名“ unsigned long”)) - ERROR: typedef redefinition with different types ('unsigned short' vs '__darwin_size_t' (aka 'unsigned long')) typedef 的重新定义 - redefinition of typedef 错误10错误C2371:&#39;print_plant&#39;:重新定义; 不同的基本类型[C] - Error 10 error C2371: 'print_plant' : redefinition; different basic types [C]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM