473,508 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class forward declaration

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
Jul 19 '05 #1
6 14462
"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:bk**********@f04n12.cac.psu.edu...
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;
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.
};

class TextDB_Version
{
[...]
};

} // namespace TextDB

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

Thanks a lot,
Kevin Grigorenko


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #2
> 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.
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...
class TextDB
{
private:
TextDB_Version database_version;
};


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
Jul 19 '05 #3
>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:
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.
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.


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
Jul 19 '05 #4
"Jonathan Mcdougall" <DE******************@yahoo.ca> wrote in message
news:7m********************************@4ax.com...
I have the definitions of classes A and B in a header file. Class A has aprivate 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:


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.
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.


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


Thanks a lot, that cleared things up clearly.

Kevin Grigorenko
Jul 19 '05 #5

"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:bk***********@f04n12.cac.psu.edu...

Thanks a lot, that cleared things up clearly.


As opposed to clearing unclearly? :-)
Jul 19 '05 #6
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:bk***********@f04n12.cac.psu.edu...

Thanks a lot, that cleared things up clearly.


As opposed to clearing unclearly? :-)


Haha, touché!
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3877
by: Jacek Dziedzic | last post by:
Hello! I'm creating a class that has a static method which would compute a lookup table needed later by all members of the class. The problem is, the lookup table is composed of records and one...
5
9039
by: Stephane Routelous | last post by:
Hi, I would like to make a forward declaration of a strcuture nested in a class. I have file A.h class A { public: struct B
5
1920
by: Xiangliang Meng | last post by:
Hi, all. What are the benefit and the drawback of defining a class embedded inside another class? For example: class List { public:
4
3388
by: jonathanho15 | last post by:
Sometimes, when I browse through some code, I see declarations like: class SomeClass; These make absolutely no sense to me. Can anyone tell me what these kind of declarations do? Thanks!
2
4755
by: blueblueblue2005 | last post by:
Hi, there was a post several days ago about using forward class declaration to solve the circular including issue, today, one question suddenly came into mind: which class should the forward class...
23
3819
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
3
8846
by: yancheng.cheok | last post by:
hello all, how can i make, a forward declaration class's enum member, being visible by another class? consider the following case, ---------------------------- dog.h...
19
17255
by: bubzilla | last post by:
Hi, i´ve got about 10 headerfiles with implemented classes. Now when i try to compile them i get the following message: In file included from Proxy/ServerCnx.hh:36, from...
17
1556
by: Amchi | last post by:
Alright .... this makes no sense ... Declared a class 'diskStorage' in a header ...diskStorage.h Defined it's contructor and methods ... in diskStorage.cpp Included diskStorage header in...
7
1445
by: Jess | last post by:
Hello, I'd like to declare two classes to be friend classes, I did the following: #include<iostream> using namespace std; class A{
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.