C++ question

Vertimus

Banned
Apr 2, 2004
1,441
0
0
For simplicity, suppose I have two files, each with one namespace containing one class. One is "Aspen.h", the other "Birch.h"

Aspen.h contains:
#ifndef H_ASPEN
#define H_ASPEN

#include "Birch.h"

namespace Aspen {
class Alpha {
public:
void FunctionFoo(Birch::Beta*);
};
};
#endif

Birch.h contains:

#ifndef H_BIRCH
#define H_BIRCH

#include "Aspen.h"

namespace Birch {
class Beta {
public:
FunctionBar(int);
private:
Aspen::Alpha* m_data;
};
};
#endif

The problem:

When I compile "Aspen.cc" (not shown), the header "Aspen.h" includes "Birch.h". "Birch.h" has a private variable named m_data of type Aspen::Alpha*. However, the compiler (gcc 3.4) gives me the following errors:
In file included from Aspen.h:4:
Birch.h:11: error: `Aspen' has not been declared

But the file I'm compiling contains the namespace Aspen, so I have no clue to why it doesn't recognize Aspen.

Any remedies?

Thanks! :p

EDIT: sorry about the spacing. ATOT removes them.
 

hypn0tik

Diamond Member
Jul 5, 2005
5,866
2
0
Shouldn't the line be:

include <Aspen.h>

Haven't programmed in C++ for a while but that's what I think it is.

Edit: Or perhaps:

#include <Aspen.h>

No clue.
 

cliftonite

Diamond Member
Jul 15, 2001
6,899
63
91
Originally posted by: Vertimus
For simplicity, suppose I have two files, each with one namespace containing one class. One is "Aspen.h", the other "Birch.h"

Aspen.h contains:
#ifndef H_ASPEN
#define H_ASPEN

#include "Birch.h"

namespace Aspen {
class Alpha {
public:
void FunctionFoo(Birch::Beta*);
};
};
#endif

Birch.h contains:

#ifndef H_BIRCH
#define H_BIRCH

#include "Aspen.h"

namespace Birch {
class Beta {
public:
FunctionBar(int);
private:
Aspen::Alpha* m_data;
};
};
#endif

The problem:

When I compile "Aspen.cc" (not shown), the header "Aspen.h" includes "Birch.h". "Birch.h" has a private variable named m_data of type Aspen::Alpha*. However, the compiler (gcc 3.4) gives me the following errors:
In file included from Aspen.h:4:
Birch.h:11: error: `Aspen' has not been declared

But the file I'm compiling contains the namespace Aspen, so I have no clue to why it doesn't recognize Aspen.

Any remedies?

Thanks! :p

EDIT: sorry about the spacing. ATOT removes them.


Try declraing Aspen::Alpha* m_data; outside the function.
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
Originally posted by: hypn0tik
Shouldn't the line be:

include <Aspen.h>

Haven't programmed in C++ for a while but that's what I think it is.

Edit: Or perhaps:

#include <Aspen.h>

No clue.

I thought triangular brackets were only used for system header files.

I changed to triangular brackets, and it now says it can't find "Birch.h", which matches my predictions.
 

hypn0tik

Diamond Member
Jul 5, 2005
5,866
2
0
Originally posted by: Vertimus
Originally posted by: hypn0tik
Shouldn't the line be:

include <Aspen.h>

Haven't programmed in C++ for a while but that's what I think it is.

Edit: Or perhaps:

#include <Aspen.h>

No clue.

I thought triangular brackets were only used for system header files.

I changed to triangular brackets, and it now says it can't find "Birch.h", which matches my predictions.


Ah, my bad. I don't know then. Sorry.
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
Originally posted by: cliftonite
Originally posted by: Vertimus
For simplicity, suppose I have two files, each with one namespace containing one class. One is "Aspen.h", the other "Birch.h"

Aspen.h contains:
#ifndef H_ASPEN
#define H_ASPEN

#include "Birch.h"

namespace Aspen {
class Alpha {
public:
void FunctionFoo(Birch::Beta*);
};
};
#endif

Birch.h contains:

#ifndef H_BIRCH
#define H_BIRCH

#include "Aspen.h"

namespace Birch {
class Beta {
public:
FunctionBar(int);
private:
Aspen::Alpha* m_data;
};
};
#endif

The problem:

When I compile "Aspen.cc" (not shown), the header "Aspen.h" includes "Birch.h". "Birch.h" has a private variable named m_data of type Aspen::Alpha*. However, the compiler (gcc 3.4) gives me the following errors:
In file included from Aspen.h:4:
Birch.h:11: error: `Aspen' has not been declared

But the file I'm compiling contains the namespace Aspen, so I have no clue to why it doesn't recognize Aspen.

Any remedies?

Thanks! :p

EDIT: sorry about the spacing. ATOT removes them.


Try declraing Aspen::Alpha* m_data; outside the function.

Outside what function?

I don't have any implementations of any functions, all i have are two declarations, none with Aspen::Alpha* m_data in them.
 

akubi

Diamond Member
Apr 19, 2005
4,392
1
0
you need to forward declare Aspen before Birch.h is included.

Aspen.cc should start off with:

namespace Aspen {
class Aspen;
};
#include "Aspen.h"

this is necessary cuz you have a cyclic dependency betw aspen and birch
 

cliftonite

Diamond Member
Jul 15, 2001
6,899
63
91
Originally posted by: Vertimus
Originally posted by: cliftonite
Originally posted by: Vertimus
For simplicity, suppose I have two files, each with one namespace containing one class. One is "Aspen.h", the other "Birch.h"

Aspen.h contains:
#ifndef H_ASPEN
#define H_ASPEN

#include "Birch.h"

namespace Aspen {
class Alpha {
public:
void FunctionFoo(Birch::Beta*);
};
};
#endif

Birch.h contains:

#ifndef H_BIRCH
#define H_BIRCH

#include "Aspen.h"

namespace Birch {
class Beta {
public:
FunctionBar(int);
private:
Aspen::Alpha* m_data;
};
};
#endif

The problem:

When I compile "Aspen.cc" (not shown), the header "Aspen.h" includes "Birch.h". "Birch.h" has a private variable named m_data of type Aspen::Alpha*. However, the compiler (gcc 3.4) gives me the following errors:
In file included from Aspen.h:4:
Birch.h:11: error: `Aspen' has not been declared

But the file I'm compiling contains the namespace Aspen, so I have no clue to why it doesn't recognize Aspen.

Any remedies?

Thanks! :p

EDIT: sorry about the spacing. ATOT removes them.


Try declraing Aspen::Alpha* m_data; outside the function.

Outside what function?



oops thought he declared inside bar :eek:

 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
Originally posted by: akubi
you need to forward declare Aspen before Birch.h is included.

Aspen.cc should start off with:

namespace Aspen {
class Aspen;
};
#include "Aspen.h"

this is necessary cuz you have a cyclic dependency betw aspen and birch

Thanks, that seems to work :p

Is there a rule of thumb to when I need to forward declare?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Vertimus
Is there a rule of thumb to when I need to forward declare?

Anytime you need a class that is not included via the header file.

Nested includes can easily create such cyclic dependencies.

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: EagleKeeper
Originally posted by: Vertimus
Is there a rule of thumb to when I need to forward declare?

Anytime you need a class that is not included via the header file.

Nested includes can easily create such cyclic dependencies.

I would actually say it a little more strongly than EagleKeeper. I think you should use a forward declaration whenever you can do it. In general, the only time you really need to include another header file is when the compiler needs to know the size of the class you're referencing while compiling the current file. It can use the forward declaration for pointers, references and template parameters. It cannot use the forward declaration for member variables that are neither pointers nor references.

The reason for this stronger stance is that reducing the dependencies between your header files is just a good thing. Yes, it helps resolve circular dependencies, but it also allows other people to use your code without pulling in a whole bunch of things they may not need.