Connecting Tech Pros Worldwide Forums | Help | Site Map

Class forward declaration

Kevin Grigorenko
Guest
 
Posts: n/a
#1: Jul 19 '05
Hello,

I have the definitions of classes A and B in a header file. Class A has a
private member of type B. Class B is defined after class A in the header
file. I place a forward declaration of class B above class A but VS.NET
still complains with:

error C2079: 'TextDB::TextDB::database_version' uses undefined class
'TextDB::TextDB_Version'

namespace TextDB
{
class TextDB_Version;

class TextDB
{
private:
TextDB_Version database_version;
};

class TextDB_Version
{
[...]
};

} // namespace TextDB

Is this because this is in a header file? I'm confused.

Thanks a lot,
Kevin Grigorenko



Cy Edmunds
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Class forward declaration


"Kevin Grigorenko" <kzg110@psu.edu> wrote in message
news:bk3dik$t1m$1@f04n12.cac.psu.edu...[color=blue]
> Hello,
>
> I have the definitions of classes A and B in a header file. Class A has a
> private member of type B. Class B is defined after class A in the header
> file. I place a forward declaration of class B above class A but VS.NET
> still complains with:
>
> error C2079: 'TextDB::TextDB::database_version' uses undefined class
> 'TextDB::TextDB_Version'
>
> namespace TextDB
> {
> class TextDB_Version;
>
> class TextDB
> {
> private:
> TextDB_Version database_version;[/color]

How much space should the compiler allocate for this class? It can't know at
this point. It would work to say

TextDB_Version *pdatabase_version;

because all pointers have the same size.
[color=blue]
> };
>
> class TextDB_Version
> {
> [...]
> };
>
> } // namespace TextDB
>
> Is this because this is in a header file? I'm confused.
>
> Thanks a lot,
> Kevin Grigorenko
>[/color]

--
Cy
http://home.rochester.rr.com/cyhome/


Phlip
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Class forward declaration


> I have the definitions of classes A and B in a header file. Class A has a[color=blue]
> private member of type B. Class B is defined after class A in the header
> file. I place a forward declaration of class B above class A but VS.NET
> still complains with:
>
> error C2079: 'TextDB::TextDB::database_version' uses undefined class
> 'TextDB::TextDB_Version'
>
> namespace TextDB
> {
> class TextDB_Version;
>
> class TextDB
> {
> private:
> TextDB_Version database_version;
> };
>
> class TextDB_Version
> {
> [...]
> };
>
> } // namespace TextDB
>
> Is this because this is in a header file? I'm confused.[/color]

C++ obeys parsing rules that behave as-if the compiler were only allowed to
"know" things that have been defined "above" where they are used.

(Also, C++'s core language parses after the preprocessor commands, so the
location of a declaration "in a header file" is irrelevant.)

As a rubric, think of the definition of a class (the {} part) as declaring a
classes "size", and think of the definition only working if the compiler
knows the size of each component object.

So, at this line...
[color=blue]
> class TextDB
> {
> private:
> TextDB_Version database_version;
> };[/color]

The compiler is trying to determine the size of TextDB. But it does not yet
know the size of TextDB_Version. So it can't define TextDB in terms of
TextDB_Version, which is not defined yet.

The solution is to put TextDB_Version above TextDB. C++'s object
construction rules take precedence - slightly - over any grandiose ideals of
perfectly encapsulated objects.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


Jonathan Mcdougall
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Class forward declaration


>I have the definitions of classes A and B in a header file. Class A has a[color=blue]
>private member of type B. Class B is defined after class A in the header
>file. I place a forward declaration of class B above class A but VS.NET
>still complains with:[/color]

For the compiler to know the exact size of a class, it must know the
exact size of its members. By putting only a declaration, the
compiler does not know the size of the class, hence the error.
[color=blue]
>error C2079: 'TextDB::TextDB::database_version' uses undefined class
>'TextDB::TextDB_Version'
>
>namespace TextDB
>{
> class TextDB_Version;
>
> class TextDB
> {
> private:
> TextDB_Version database_version;
> };
>
> class TextDB_Version
> {
> [...]
> };
>
>} // namespace TextDB
>
>Is this because this is in a header file? I'm confused.[/color]

Take a look :

class A
{
int i;
};

Here the compiler knows 'A' will have the size
sizeof(int) + implementation_defined_things

In your class :

class TextDB
{
private:
TextDB_Version database_version;
};

the compiler tries the same thing :
sizeof(TextDB_Version) + implementation_defined_things

and just cannot find the size of TextDB_Version since you only
declared it.

You have got two choices :

1) Define the class TextDB_Version *before* TextDB

2) Make database_version a pointer and allocate the
memory dynamically


Jonathan
Kevin Grigorenko
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Class forward declaration


"Jonathan Mcdougall" <DELjonathanmcdougall@yahoo.ca> wrote in message
news:7miamvs2nel97g96r17sccjc1abeivs2p0@4ax.com...[color=blue][color=green]
> >I have the definitions of classes A and B in a header file. Class A has[/color][/color]
a[color=blue][color=green]
> >private member of type B. Class B is defined after class A in the header
> >file. I place a forward declaration of class B above class A but VS.NET
> >still complains with:[/color]
>
> For the compiler to know the exact size of a class, it must know the
> exact size of its members. By putting only a declaration, the
> compiler does not know the size of the class, hence the error.
>[color=green]
> >error C2079: 'TextDB::TextDB::database_version' uses undefined class
> >'TextDB::TextDB_Version'
> >
> >namespace TextDB
> >{
> > class TextDB_Version;
> >
> > class TextDB
> > {
> > private:
> > TextDB_Version database_version;
> > };
> >
> > class TextDB_Version
> > {
> > [...]
> > };
> >
> >} // namespace TextDB
> >
> >Is this because this is in a header file? I'm confused.[/color]
>
> Take a look :
>
> class A
> {
> int i;
> };
>
> Here the compiler knows 'A' will have the size
> sizeof(int) + implementation_defined_things
>
> In your class :
>
> class TextDB
> {
> private:
> TextDB_Version database_version;
> };
>
> the compiler tries the same thing :
> sizeof(TextDB_Version) + implementation_defined_things
>
> and just cannot find the size of TextDB_Version since you only
> declared it.
>
> You have got two choices :
>
> 1) Define the class TextDB_Version *before* TextDB
>
> 2) Make database_version a pointer and allocate the
> memory dynamically
>
>
> Jonathan[/color]

Thanks a lot, that cleared things up clearly.

Kevin Grigorenko


jeffc
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Class forward declaration



"Kevin Grigorenko" <kzg110@psu.edu> wrote in message
news:bk4eo0$1ctk$1@f04n12.cac.psu.edu...[color=blue]
>
> Thanks a lot, that cleared things up clearly.[/color]

As opposed to clearing unclearly? :-)


Kevin Grigorenko
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Class forward declaration


"jeffc" <nobody@nowhere.com> wrote in message
news:3f65d848_4@news1.prserv.net...[color=blue]
>
> "Kevin Grigorenko" <kzg110@psu.edu> wrote in message
> news:bk4eo0$1ctk$1@f04n12.cac.psu.edu...[color=green]
> >
> > Thanks a lot, that cleared things up clearly.[/color]
>
> As opposed to clearing unclearly? :-)
>
>[/color]

Haha, touché!


Closed Thread