473,809 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaration vs definition w/classes

I have a few quetions about definitions vs declarations concerning
defined types (user defined), specifically classes. From what I
understand, a class contained in a header file is a definition as a
whole, but the contents a.k.a members, are the declarations (non-inline
functions). What I'm trying to understand, is, is storage allocated or
not allocated for them at this phase? Next, the members implemented in
a .cpp file, for the class, are their storage allocated for them during
compilation, or is this done in the class definition (header file)? I'm
trying to figure out a problem (contained below) but I first need these
questions cleared for me, Thanks:

The following compiles fine when in the "two.h" file, a forward
declaration (class ClassOne;), is used, without #include "one.h", but,
when I try removing the forward declaration and this time only #include
"one.h", I get 2 errors. I initially thought that both were merely the
same, for one because a forward delcaration is an incomplete type, and
resolves during link time, so I see how there's no problem there.
However, I thought that including the header file instead of the
forward declaration would bring about the same error-free compilation
because the definition, which is the class, is being inserted at the
top, where the include directive is at, and therefore, a definition =
better than delclaration because it is "not" an incomplete type and
rather, it has been allocated storage. Where am I going wrong?

#ifndef ONE_H
#define ONE_H
#include "two.h"

class ClassOne
{

public:
ClassOne();
friend void ClassTwo::Displ ayClassName(Cla ssOne*);
};
#endif
#ifndef TWO_H
#define TWO_H
#include "one.h"

class ClassOne;
class ClassTwo
{

public:
void DisplayClassNam e(ClassOne*);
};
#endif

Apr 8 '06 #1
2 2697
xl************* **@gmail.com wrote:
I have a few quetions about definitions vs declarations concerning
defined types (user defined), specifically classes. From what I
understand, a class contained in a header file is a definition as a
whole, but the contents a.k.a members, are the declarations
(non-inline functions). What I'm trying to understand, is, is storage
allocated or not allocated for them at this phase? Next, the members
implemented in a .cpp file, for the class, are their storage
allocated for them during compilation, or is this done in the class
definition (header file)? I'm trying to figure out a problem
(contained below) but I first need these questions cleared for me,
You do not need to know that. And acutally you cannot, because every
compiler will allocate storage whenever it wants to. You do need to know
tho, when an incomplete type is sufficient and when it is not.
The following compiles fine when in the "two.h" file, a forward
declaration (class ClassOne;), is used, without #include "one.h", but,
when I try removing the forward declaration and this time only
#include "one.h", I get 2 errors. I initially thought that both were
merely the same, for one because a forward delcaration is an
incomplete type, and resolves during link time, so I see how there's
no problem there. However, I thought that including the header file
instead of the forward declaration would bring about the same
error-free compilation because the definition, which is the class, is
being inserted at the top, where the include directive is at, and
therefore, a definition = better than delclaration because it is
"not" an incomplete type and rather, it has been allocated storage.
Where am I going wrong?

#ifndef ONE_H
#define ONE_H
#include "two.h"

class ClassOne
{

public:
ClassOne();
friend void ClassTwo::Displ ayClassName(Cla ssOne*);
At this point you need the definition of the class. That is, the
compiler needs to know, that this function exists in 'ClassTwo'.
};
#endif
#ifndef TWO_H
#define TWO_H
#include "one.h"

class ClassOne;
class ClassTwo
{

public:
void DisplayClassNam e(ClassOne*);
Here the class declaration is sufficient, because you are not using
the definition. All the compiler needs to know is, that 'ClassOne'
exists.
};
#endif


Now when you include 'one.h' in 'two.h', take a look at what is
actually happening:

#ifndef TWO_H
#define TWO_H

#include "one.h" --> expands to:
#ifndef ONE_H
#define ONE_H
#include "two.h" --> expands to:
#ifndef TWO_H
// TWO_H was already defined,
// so it's not included again
#endif
class ClassOne
{

public:
ClassOne();
friend void ClassTwo::Displ ayClassName(Cla ssOne*);
};
#endif
class ClassTwo
{

public:
void DisplayClassNam e(ClassOne*);
};
#endif

You will see that 'ClassOne' needs the definition of 'ClassTwo',
which is given at a later point. Too late for it to compile.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 8 '06 #2
xl************* **@gmail.com wrote:
I have a few quetions about definitions vs declarations concerning
defined types (user defined), specifically classes. From what I
understand, a class contained in a header file is a definition as a
whole, but the contents a.k.a members, are the declarations (non-inline
functions). What I'm trying to understand, is, is storage allocated or
not allocated for them at this phase? Next, the members implemented in
a .cpp file, for the class, are their storage allocated for them during
compilation, or is this done in the class definition (header file)? I'm
trying to figure out a problem (contained below) but I first need these
questions cleared for me, Thanks:


What do you mean by "storage"? Please be more specific.

Generally, if you have a class declaration, then you can refer to a
pointer or reference of that class:

class Line;

Line& foo();

Line* pline; // OK
Line& refline = foo(); // OK

Line line; // Error: unknown sizeof(Line);

With a full definition of the class you will be able to use the object
of that class:

class Line{
public:
Point pt1, pt2;

void bar();
};

Line line; // Ok
line.pt1 = Point(12, 24); // Ok

And finally, we need the definition of the bar to link successfully.

Regards
Ben
Apr 8 '06 #3

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

Similar topics

6
14492
by: Kevin Grigorenko | last post by:
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'
3
2056
by: Martin Eisenberg | last post by:
Hi! I have a header with two classes, WidthLogger and Hyst. WidthLogger is declared first; its ctor takes a Hyst reference. Hyst declares WidthLogger a friend. Hyst constructs a WidthLogger member, giving itself as parameter, and calls its log() method in various places. WidthLogger::log() then uses the reference to note down the values of some Hyst members. Except that I don't get that far because VC6 is bitchin', I hope
3
20615
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward declaration? class CEnemy : public CEntity { public:
10
20899
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. sourcefile1.c ============== extern long globalfoo; /* declaration? */
3
11677
by: François Côté | last post by:
I'm generating a TLB from one of my assemblies that contains an interface declaration. The class that implements this interface is in another assembly. I import this tlb in my C++ code to create instances of my C# classes. The problem I have is that I need to specify the class in my CoCreate call in C++. If I try to generate a TLB for the assembly that does contains the class and interface implementation, I end up with a circular...
3
2034
by: Henning Hasemann | last post by:
Hi all, On larger projects I used the following 'technique' (well in fact its a really simple thing): 1. Write one source-code file for each 'large' class and one corresponding header file. (code in source-code file declaration in header file, as usual) 2. Write a central header file which first holds forward declarations of all classes an then includes all other header files.
9
8903
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
14
13073
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
26
14790
by: ducttape | last post by:
myProgram.cpp: class childClass { parentClass parent; //reference to class that created this child childClass(parentClass p) { parent = p; } };
0
10635
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
10376
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
10378
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
10115
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...
0
9198
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
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...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
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.