I have a Parser.h, that defines a struct StmtParent:
...
struct StmtParent;
class Parser {
...
Then in Parser.cpp:
struct StmtParent {
int stmtNo;
int parent;
};
...
Seems all right? Then I have a unit test (cppunit):
# in ParserUnitTests.h
#include "header\Parser.h"
# in ParserUnitTests.cpp
void ParserUnitTests::testParseProcSideEffects() {
...
stack<StmtParent> follows;
...
Then I get errors like:
error C2027: use of undefined type 'StmtParent'
Why, I can use functions like Parser::parseLine(). Why can’t I access the struct? So I tried including Parser.h in ParserUnitTests.cpp (although I already have it included in the header). Then I get:
Error 8 error C2146: syntax error : missing ';' before identifier 'm_cCurToken' c:\program files (x86)\microsoft sdks\windows\v7.0a\include\parser.h 52
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft sdks\windows\v7.0a\include\parser.h 52
Error 10 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft sdks\windows\v7.0a\include\parser.h 52
...
Parser.hdoes not define the struct, it forward-declares it. Therefore it’s incomplete when you try to use it as a template parameter for thestack, and you can’t use incomplete types as parameters for STL containers:You can check this out for reasoning.