Getting "Error: incomplete type is not allowed"...

...when declare and assign an instance of a user-defined struct in a function. And the struct (theStruct) is not declared in the same header file as the function (theFunction). Like this:

files:
"A.h": declares the struct in a class (theClass)
"A.cpp": implements the struct
"B.h": declares the function
"B.cpp": implements the function, error here

I think making the instance (inst) a reference might solve this. But the instance is assigned to a return value from a function (returnFunc). Like:
1
2
3
4
5
6
7
void theFunction() {
...
theClass::theStruct inst = returnFunc(...); 
//returnFunc() returns an instance of theStruct
//the error is at 'inst'
...
}

What do you think?
The definition of theClass::theStruct must be visible to theFunction.
Sure it's visible, it's public. I'm getting "incomplete ype is not allowed" error.
When you say
"A.h": declares the struct in a class
are you just declaring the struct? ie struct theClass ? If so then you need to define the whole class in A.h (you can define its functions elsewhere).
This is because B.cpp doesn't know what the struct is (it knows it's a struct because you declared it, but not how big it is or what it's members are)
dleanjeanz wrote:
Sure it's visible, it's public. I'm getting "incomplete ype is not allowed" error.

I thought I was supplying enough emphasis. Let me add a little more:
cire wrote:
The definition of theClass::theStruct must be visible to theFunction.

A declaration is insufficient.
OK. I've edited:
1
2
3
4
5
class theClass {
...
struct theStruct;
...
}

to
1
2
3
4
5
6
7
class theClass {
...
struct theStruct {
...
}
...
}


Thanks a lot.
r
Topic archived. No new replies allowed.