I have a code,
class foo : public bar
{
public:
foo(){};
~foo(){};
};
class wu
{
public:
wu(const bar& Bar ) :
m_bar(Bar)
{};
~wu(){};
private:
bar m_bar;
};
int main()
{
foo tmpFoo;
wu tmpWu(tmpFoo);
}
Now my problem is, the code above will not compile and the error message is “error: variable wu tmpWu has initializer but incomplete type”.
Does it mean, I have to cast the tmpFoo object to bar class?
Please advice.
Thanks.
You must use the syntax
m_bar(Bar)instead ofm_bar = Barin thewuclass constructor. Also, remove the braces from thetmpFoovariable declaration, otherwise you will be declaring a function that returns afooobject and receives no arguments.After your edit: I tried that code, and the problem it gave was that the
barclass was undefined. In your case, the compiler gave an “incomplete type” error; that means that somewhere in an included file (or in the same file), the classbaris declared this way:but it is never defined its contents.