tags:

views:

94

answers:

2

Hi,

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.

+5  A: 

You must use the syntax m_bar(Bar) instead of m_bar = Bar in the wu class constructor. Also, remove the braces from the tmpFoo variable declaration, otherwise you will be declaring a function that returns a foo object and receives no arguments.


After your edit: I tried that code, and the problem it gave was that the bar class 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 class bar is declared this way:

class bar;

but it is never defined its contents.

jbernadas
Thanks, I just edited.
sasayins
Thank your very much, but please assume the the bar was defined.
sasayins
@sasayins: If `bar` is defined as say `class bar {};` it builds fine...
Georg Fritzsche
+4  A: 

adding

class bar {};

your code works for me. Am I missing something?

bronzebeard