Newbie question: classes "A" and "B". A "B" object declared inside class "A"

Holger Blasum hbl@sysgo.com
Thu Aug 9 19:49:00 GMT 2007


On Thu, Aug 09, 2007 at 11:48:32AM -0700, tirengarfio wrote:
> I have declared two classes: class CPrueba and class CPrueba2.
> The class CPrueba contains a CPrueba2 object.
> When i try to compile a main.cpp file with a CPrueba object inside using 
> "g++ main.cpp CPrueba.cpp CPrueba2 -o main"  i get this error:
> ???CPrueba2??? no nombra a un tipo (In spanish means 'CPrueba2 doesn't name a
> type' or maybe "CPrueba2 is not a type").

CPrueba.h does not anticipate that you define CPrueba2 in CPrueba2.h unless
you tell it so. (I think, correct me if I am wrong, this is not a 
property of the GCC compiler collection but of the C++ standard, 
e.g. the C standard says: "A source file together with all the 
headers and source files included  via the preprocessing directive 
#include is known as a preprocessing translation unit.", WG14/N794 
J11/97-158)

So e.g. change headers to:

#### CPrueba2.h

#ifndef CPRUEBA2_H
#define CPRUEBA2_H
class CPrueba2
{
private:
int n;
};
#endif

#### CPrueba.h

#include "CPrueba2.h"
class CPrueba
{
private:
CPrueba2 pru2;
};

(For exercise, first simply do the change to CPrueba.h below and 
observe the error messages you get.)

You may want to read up on headers e.g. in Chapter 4 of Eckels 
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Saludos,

-- 
Holger Blasum / SYSGO AG / http://www.sysgo.com/



More information about the Gcc-help mailing list