domlao
domlao

Reputation: 16029

How to properly cast an object?

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.

Upvotes: 0

Views: 154

Answers (2)

Rohan Monga
Rohan Monga

Reputation: 1777

adding

class bar {};

your code works for me. Am I missing something?

Upvotes: 4

jbernadas
jbernadas

Reputation: 2600

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.

Upvotes: 5

Related Questions