2012-07-19 2 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

답변

1

myotherstruct 식별자가 struct 태그 아니다 선언해야합니다. struct 키워드없이 사용하십시오. 정의한 후에는 struct 태그에 대해 이름을 재사용 할 수 없습니다. 귀하의 예에서는 myotherstruct 유형을 전달 선언하지 않으므로 myotherstruct 태그를 사용하여 struct을 전달 선언합니다. typedef에 대해 이미 myotherstruct 이름이 사용 되었기 때문에 오류가 발생합니다.

+0

struct 태그가 유형 이름과 동일합니까? mystruct m; –

+0

@LeonhartSquall이 질문에 대한 답변은 C++인지 C++인지에 따라 달라집니다. C++에서는 구조체의 태그를'typedef '없이 이름으로 사용할 수 있지만 C에서는 먼저 typedef해야합니다. – dasblinkenlight

+0

구조체 태그는 mystruct를 사용하여 객체를 정의 할 때 유형이? 인 것과 같습니다. mystruct m; 나는 항상 mystruct가 타입 이름이라고 생각한다. 그리고 그것은 구조체 태그라는 것을 결코 알지 못합니다. 구조체 태그를 언변 할 수있는 방법은 무엇입니까? –

1

typedefs은 정형 선언 된 struct의 전달 선언없이 전달할 수 없습니다. 먼저 앞으로가 자신의 권리에 유형 이름되면, struct 다음 typedef

struct mystruct; 
typedef mystruct myotherstruct;