2012-07-19 181 views
6
struct mystruct 
{ 
    int i; 
    double f; 
} ; 

typedef mystruct myotherstruct; 

//the other .cpp file 
struct mystruct; //OK,this is a correct forward declaration. 
struct myotherstruct; // error C2371(in vc2k8): 'myotherstruct' : redefinition; different basic types 

大家好。 为什么我不能转发myotherstruct?声明typedef类型

+1

http://stackoverflow.com/questions/804894/forward-declaration-of-a-typedef-in-c – Jeeva 2012-07-19 10:47:25

回答

1

myotherstruct标识符不是struct标签,它是在其自己的权利类型名称。您在没有struct关键字的情况下使用它。一旦定义,该名称就不能再用于struct标签。在你的例子中,你没有向前声明myotherstruct类型,你正在向前宣布一个struct的标签myotherstruct,这会给你一个错误,因为名称myotherstruct已经被用于typedef

+0

是一个等价于类型名称的结构标签吗? mystruct m; – 2012-07-19 11:10:21

+0

@LeonhartSquall奇怪的是,这个问题的答案取决于这是C++还是C:在C++中,结构的标签可以用作不带'typedef'的名称,而在C中,你必须首先'typedef'。 – dasblinkenlight 2012-07-19 11:12:13

+0

当我使用mystruct定义一个对象时,是一个等同于类型名称的结构标记? : mystruct m; 我总是认为mystruct是一个类型名称。我从来不知道它是一个结构标签。我可以如何下标结构标签。 – 2012-07-19 11:16:16

1

如果没有对typedefed的struct进行前向声明,则无法转发声明typedefs。你应该先向前声明struct然后typedef

struct mystruct; 
typedef mystruct myotherstruct;