[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]
Tough #include Problem
-- Exitus Acta Probat --
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
but there must be a way arround there, no?
[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]
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]
don''t know why though....
[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]
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.
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-]
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-]
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 --
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-]
The other should declare 1 as friend class 1;
.lick
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 ...
// 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]
oh, by the way, i got it to work with your help, thnx guys!
[Hugo Ferreira][Positronic Dreams]
Need [3D Artist] & [Sound Designer]
for small project. Contact me plz...
[-NO TO WAR ON IRAQ-]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.