C++ forward declaration Help!!!!

T

thegreatgonzo

Hi i am not very skilled in forward declarations. I have this program:

class C
{
public:
C()
{
//Do something usefull
}
};

class B;

class A : public C
{
public:
A(B *parent)
{
myParent = parent;
if(parent->number == 10)
return;
}

B *myParent;
};

class B : public C
{
public:
B()
{
number = 5;
}
int number;
};

int main()
{
B myB;
myB.number = 10;
return 0;
}

this code produces the following errors:

test3.cpp: In constructor ‘A::A(B*)’: test3.cpp:18: error: invalid use of
undefined type ‘struct B’ test3.cpp:10: error: forward declaration of
‘struct B’

on g++ 4.1.2, running ubuntu;

can anyone help me please?

grz,

Leon
 
W

werasm

Hi i am not very skilled in forward declarations. I have this program:

class C
{
public:
C()
{
//Do something usefull
}

};

class B;

class A : public C
{
public:
A(B *parent)
{
myParent = parent;
if(parent->number == 10)
return;
}

B *myParent;

};

class B : public C
{
public:
B()
{
number = 5;
}
int number;

};

int main()
{
B myB;
myB.number = 10;
return 0;

}

this code produces the following errors:

test3.cpp: In constructor 'A::A(B*)': test3.cpp:18: error: invalid use of
undefined type 'struct B' test3.cpp:10: error: forward declaration of
'struct B'

on g++ 4.1.2, running ubuntu;

can anyone help me please?

The modified code compiles. note the additions.
A forward declaration is not a definition. It
only introduces a name into the current scope.
Therefore is could be used to resolve name
dependencies only. For this reason you need to
implement the constructor in a cpp file (or at
least below the definition of the forward declared
class) for things to compile.

BTW, I've omitted included guards. You could consider
adding those.

Regards,

Werner

// FILE C.h
class C
{
public:
C()
{
//Do something usefull
}

};

// FILE A.h
#include "C.h"

class B;
// NOTE:
// Definition of B not available, therefore
// members are not known to the compiler.
class A : public C
{
public:
A(B *parent);

B *myParent;

};

// FILE B.h
#include "C.h"
class B : public C
{
public:
B()
{
number = 5;
}
int number;

};

// FILE A.cpp
#include "A.h"
#include "B.h"

#include <iostream>
A::A(B *parent)
{
std::cout << parent->number << std::endl;
}


int main()
{
B myB;
myB.number = 10;
return 0;
}
 
T

thegreatgonzo

The modified code compiles. note the additions.
A forward declaration is not a definition. It
only introduces a name into the current scope.
Therefore is could be used to resolve name
dependencies only. For this reason you need to
implement the constructor in a cpp file (or at
least below the definition of the forward declared
class) for things to compile.

BTW, I've omitted included guards. You could consider
adding those.

Regards,

Werner

// FILE C.h
class C
{
public:
C()
{
//Do something usefull
}

};

// FILE A.h
#include "C.h"

class B;
// NOTE:
// Definition of B not available, therefore
// members are not known to the compiler.
class A : public C
{
public:
A(B *parent);

B *myParent;

};

// FILE B.h
#include "C.h"
class B : public C
{
public:
B()
{
number = 5;
}
int number;

};

// FILE A.cpp
#include "A.h"
#include "B.h"

#include <iostream>
A::A(B *parent)
{
std::cout << parent->number << std::endl;
}


int main()
{
B myB;
myB.number = 10;
return 0;
}

Ok it worked! Thank you times!

Regards,

Leon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top