Question

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
...
Was it helpful?

Solution

Parser.h does not define the struct, it forward-declares it. Therefore it's incomplete when you try to use it as a template parameter for the stack, and you can't use incomplete types as parameters for STL containers:

C++11 draft 3035, 17.4.3.6, paragraph 2:

In particular, the effects are undefined in the following cases:

...

if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.

You can check this out for reasoning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top