Skip to main content
GameDev.net gamedev.net
🔒 Locked

Tough #include Problem

Started by Prozak Mar 9, 2003 at 8:55 AM 13 replies 500+ views
Original Post
Prozak
Prozak
Well, im about to give up on this stupid problem: File Green #include Violet class giraf { stuff } File Violet #include Green class elephant { giraf myanimal; }; Well, this thing is giving me "storage Identifier" errors (C2146 & C2501)... I''ve never stumbled upon these strange bests before, what do i do?

[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]

jperalta
jperalta
First I think ''giraf'' is actually ''giraffe,'' second I think you should not include violet in green.

-- Exitus Acta Probat --
AndyOxfeld
AndyOxfeld
Right. It''s generally "bad form" to #include a file inside a .H file; and, if it gets out of hand, can really slow down compile times. Eliminate all the #includes in your .H files and do something like this:

class giraf;

class elephant
{
giraf myanimal;
};

The line "class giraf;" is a class declaration, sort of like a function declaration, which says that the declaration for class giraf will be coming at a later time.

- Andy Oxfeld
Nosnibor
Nosnibor
You could create another .h file that includes both, then include that in the other files:

File Blue
#include Green
#include Violet

...then #include Blue in both Green and Violet.

But you know, I don't think that works in the Green and Violet .h files... it would work for the .cpp files though. To be honest, if Green and Violet depend on each other that much you might want to re-think your class organization or just put them both in the same header.

[edited by - Nosnibor on March 9, 2003 10:34:46 AM]
cshireman
cshireman
Try using the following pre-compiler directives in your .h files.

File green.h
-----------------------------
#ifndef GREEN_H
#define GREEN_H

//Green code goes here

#endif

File violet.h
----------------------------
#ifndef VIOLET_H
#define VIOLET_H

//Violet code goes here

#endif

By doing this, you tell the compiler not to include the class definition more than once when compiling. This should make this at least a little better.
"Do or do not, there is no try." -- Yoda
Prozak
Prozak
well, i did that before reading your post,
i have the following:

#ifndef _green_10922
#define _green_10922
#pragma message("green was included")

//code

#endif

and i have a similar thing in violet...
the strange thing is that i get a few "... was included"
messages in my debugger window when building...

shouldn''t i see just one?
i''m starting to feel a complete newbie all over again...

[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]

Prozak
Prozak
jperalta, its "bad form" to include files in .h files?

if i''m declaring a class in an .h file, and if that class
uses other classes, how can i do it with a few includes
to the other classes'' files in my class'' .h file?

[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]

jperalta
jperalta
First: I never said that, second, what I did say was:

violet.h:

#include "green.h"
class Elephant {
Giraffe myanimal;
};

green.h:

class Giraffe {
...
};

There''s no need to include violet in green because the elephant is never referred to in green.

-- Exitus Acta Probat --
Prozak
Prozak
ok. My problem is that my giraf class defines a variable of type:
elephant *Cptr; // class pointer

therefore i must include it in the other file, therefore
creating a loop. But there are ways arround this...

[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]

Pipo DeClown
Pipo DeClown
let 1 include the other, but the other shouldn''t include the 1.
The other should declare 1 as friend class 1;

.lick
Xai
Xai
Ok ... STOP ... and back up ..

First, you only NEED to include a header file for class B inside another header file (for class A) if one of the following is true:

class A is derived from class B
class A CONTAINS an instance of class B
class A has inline functions or macros which use instances of class B

Otherwise, you can FOWARD DECLARE class B in the class A header file, and only include the class B header in the class A implementation (usually .cpp) file.

like this


  
// A.h file


// forward declaration

class B;

// type declaration

class A
{
public:
// stuff

protected:
B *b;
// more stuff

}

// A.cpp file


// includes

#include "A.h"
#include "B.h"

// function definitions

// ...


// B.h file


// forward declaration

class A;

// type declaration

class B
{
public:
// stuff

protected:
A *a;
// more stuff

};

// B.cpp file


// includes

#include "B.h"
#include "A.h"

// function definitions



hope that helps clear it up ... so you understand, what you have is a CIRCULAR dependency, and the only way to clear it is with FORWARD DECLARATIONS, or a redesign ...


Redleaf
Redleaf
If it''s only a pointer, you don''t need the class definition, and therefore don''t need to include the elephant header in the giraffe header.


// just use a pre-declaration
class elephant;

class giraffe
{
elephant* ptr;
};




Choose Freedom

Free Compiler and IDE:
[MinGW] [V IDE]
The Standard Template Library and Boost:
[boost] [STL] [STLport]
Free Media Libraries:
[SDL] [OpenGL] [OpenAL] [DevIL]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.