473,287 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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::DisplayClassName(ClassOne*);
};
#endif
#ifndef TWO_H
#define TWO_H
#include "one.h"

class ClassOne;
class ClassTwo
{

public:
void DisplayClassName(ClassOne*);
};
#endif

Apr 8 '06 #1
2 2662
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::DisplayClassName(ClassOne*);
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 DisplayClassName(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::DisplayClassName(ClassOne*);
};
#endif
class ClassTwo
{

public:
void DisplayClassName(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
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...
3
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...
3
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...
10
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...
3
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...
3
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....
9
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...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
26
by: ducttape | last post by:
myProgram.cpp: class childClass { parentClass parent; //reference to class that created this child childClass(parentClass p) { parent = p; ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.