473,661 Members | 2,421 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_vers ion' uses undefined class
'TextDB::TextDB _Version'

namespace TextDB
{
class TextDB_Version;

class TextDB
{
private:
TextDB_Version database_versio n;
};

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 14478
"Kevin Grigorenko" <kz****@psu.edu > wrote in message
news:bk******** **@f04n12.cac.p su.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_vers ion' uses undefined class
'TextDB::TextDB _Version'

namespace TextDB
{
class TextDB_Version;

class TextDB
{
private:
TextDB_Version database_versio n;
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_vers ion;

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_vers ion' uses undefined class
'TextDB::TextDB _Version'

namespace TextDB
{
class TextDB_Version;

class TextDB
{
private:
TextDB_Version database_versio n;
};

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_versio n;
};


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_vers ion' uses undefined class
'TextDB::TextD B_Version'

namespace TextDB
{
class TextDB_Version;

class TextDB
{
private:
TextDB_Version database_versio n;
};

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_versio n;
};

the compiler tries the same thing :
sizeof(TextDB_V ersion) + 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_versio n a pointer and allocate the
memory dynamically
Jonathan
Jul 19 '05 #4
"Jonathan Mcdougall" <DE************ ******@yahoo.ca > wrote in message
news:7m******** *************** *********@4ax.c om...
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_vers ion' uses undefined class
'TextDB::TextD B_Version'

namespace TextDB
{
class TextDB_Version;

class TextDB
{
private:
TextDB_Version database_versio n;
};

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_versio n;
};

the compiler tries the same thing :
sizeof(TextDB_V ersion) + 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_versio n 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.n et...

"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
3895
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 of the record fields is the class itself, i.e. I'm aiming at something like: typedef struct { my_class instance;
5
9082
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
1930
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
3395
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
4765
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 declaration statement be in? suppose I have a class A and B, sample code as following: // A.h #ifndef A_H #define A_H
23
3841
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
3
8855
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
17270
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 Proxy/Payload.hh:30, ...
17
1570
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 a another class ...(server.h) and then tried to declare a variable of type 'diskStorage' ... like this ( inside server.h ) diskStorage local_disk;
7
1453
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
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.