Note that there are some explanatory texts on larger screens.

plurals
  1. POMysterious compiler errors with C++ header files referencing each other
    primarykey
    data
    text
    <p>OK, this is stumping me. Relative C++ noob, but long experience with C# and other languages. </p> <p>Here's a relatively simple distillation of the problem files:</p> <pre><code>/* GameObject.h */ #pragma once #include &lt;vector&gt; class GameObject { public: std::vector&lt;Component *&gt; *Components; GameObject(); ~GameObject(); }; /* GameObject.cpp */ #include "GameObject.h" #include "Component.h" GameObject::GameObject() { } GameObject::~GameObject() { } /* Component.h */ #pragma once class Component { public: GameObject *Owner; Component(); ~Component(); }; /* Component.cpp */ #include "GameObject.h" #include "Component.h" Component::Component() { } Component::~Component() { } </code></pre> <p>This generates 21 totally irrelevant errors in Visual C++ 2012, I guess stemming from the fact that it couldn't compile Component:</p> <pre><code>C2065: 'Component' : undeclared identifier gameobject.h 10 C2059: syntax error : '&gt;' gameobject.h 10 C2143: syntax error : missing ';' before '}' gameobject.h 14 C2143: syntax error : missing ';' before '{' component.h 3 C2143: syntax error : missing ';' before '}' component.h 11 C2143: syntax error : missing ';' before '{' gameobject.cpp 8 C2143: syntax error : missing ';' before '}' gameobject.cpp 9 C2143: syntax error : missing ';' before '{' gameobject.cpp 13 C2143: syntax error : missing ';' before '}' gameobject.cpp 14 C2143: syntax error : missing ';' before '}' gameobject.cpp 16 C1004: unexpected end-of-file found gameobject.cpp 16 C2065: 'Component' : undeclared identifier gameobject.h 10 C2059: syntax error : '&gt;' gameobject.h 10 C2143: syntax error : missing ';' before '}' gameobject.h 14 C2143: syntax error : missing ';' before '{' component.h 3 C2143: syntax error : missing ';' before '}' component.h 11 C2653: 'Component' : is not a class or namespace name component.cpp 8 C2143: syntax error : missing ';' before '{' component.cpp 8 C2143: syntax error : missing ';' before '}' component.cpp 9 C2653: 'Component' : is not a class or namespace name component.cpp 13 C1903: unable to recover from previous error(s); stopping compilation component.cpp 13 </code></pre> <p>Any ideas? It makes sense in the design for Component to have a pointer to GameObject, and GameObject to have a vector of pointers to Components, so I'm not about to rearchitect to avoid that. I'm guessing I'm just doing something wrong with the header files.</p> <p>Thanks in advance for any ideas, this one's driving me crazy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload