PDA

View Full Version : c++ related



T2k
06-25-2001, 12:50 AM
i dont know how to solve this(example may work, not tried, only to show the problem ...):

FILE1.h:
#ifndef __file2__
#include "file2.h"
#define __file1__
struct xxx{
yyy *abc;
};

FILE2.h:
#ifndef __file1__
#include "file1.h"
#define __file2__
struct yyy{
xxx *tst;
};

the problem is, that the compiler say, that he dont find the class/struct, and i dont know how to say the compiler that there is a class/struct with this name, because by adding some predefinitions like "struct xxx;" or "struct yyy;" the compiler says that i try to use an undefined struct/class !?!

Tron
06-25-2001, 01:38 AM
if i understood your prob correct u'll have a pointer in struct xxx of type yyy and vice versa.
try this:




struct yyy; // declare struct yyy, so we can point at it

struct xxx
{
yyy* abc;
};

struct yyy
{
xxx* tst;
};

that should work

btw: that's a topic for a c/c++-forum, not for OpenGL

[This message has been edited by Tron (edited 06-25-2001).]

T2k
06-25-2001, 11:35 PM
no its not that easy, its a little bit complicated but i have found my problem (i think), because i have found out that the compiler includes includefiles reversely, and therefore i have to declare my funtions before they are used, but after other includes that uses it( more than two files), and with pointers it works with declaring them, because the compiler know the size of a pointer ...

ist like this(i have done a special includefile...):

includes.h:
#ifndef __file1__
#include "file1.h"
#ifndef __file2__
#include "file2.h"
//if this part is not after the files(-includes) that
use it it will produce errors
#ifndef __fileimp__
#include "fileimportant.h"
//
file1:
#define __file1__
#include "includes.h"

struct yyy{
abc here;
};

file1:
#define __file2__
#include "includes.h"

struct xxx{
abc hereto;
yyy file1;//this should produce an error, because file 2 is after file 1 in includes.h
};

fileimportant.h:
#define __fileimp__
#include "includes.h"
struct abc{

};

i hope this is correctly, because i have not found any descriptions about this in c++ docs/books

Deiussum
06-26-2001, 05:00 AM
...that the compiler includes includefiles reversely, and therefore i have to declare my funtions before they are used, but after other includes that uses it( more than two files), and with pointers it works with declaring them, because the compiler know the size of a pointer ...

Whoa! Something doesn't sound right in there. Compilers should not include files "reversely" if I understand correctly what you mean by that.

A short explanation seems to be in order. Compilers take .c and .cpp files and turn them into object code. The linker than takes the object code for all these "objects" and "links" them together into a single binary file like an executable.

Header files are not directly turned into object code. Rather they are included into the .c/.cpp files as though you had just cut and pasted the whole include file at the point where they are included. Seeing as the c/cpp files are compiled top to bottom, things need to be defined or declared before the compiler knows anything about them.

Now if you need to have two structs/classes that each have an instance of the other, you must pre-declare one of them before it is used in the other. When you do this however, the class you use the pre-declared class in can only be a pointer to that class point because it has not yet been defined so the compiler knows nothing of its size. For instance this will not work...




class A;

class B
{
public:
A a;
};

class A
{
public:
B b;
};


class A has been declared before it was used, but it has not been defined, so it will produce a compiler error trying to use it in class B. However if you change it to use a pointer like so...




class A;

class B
{
public:
A *a;
};

class A
{
public:
B b;
};


It will compile fine because it is a pointer, and the compiler knows what the fixed size for a pointer is.

I'd have to take a look at your actual code a lot closer, but I'm fairly certain that there is a much easier way of doing things than trying to use all the #ifndef precompiler stuff.

T2k
06-26-2001, 10:48 PM
hmm, that something like this:
struct a{
b B;
};
struct b{
a A;
};

dont work is easy to understand( because this would build a infinite sized stuct, in which each struct contains the other, infinite times), but i know what my code is, and i know that dependent on the order of including files(which will be inserted to the cpp files as code) the compiler shows me errors or not, and the errors only appear if i theoreticaly do this:

class B;
class A{
B *b;
};
class B{
A a;
};

so i dont understand what the f*** compiler does ..., maybe a bug ?

ffish
06-27-2001, 12:46 AM
What compiler do you use? This code compiles and runs fine on my machine using VC++6:



class B;

class A {
B* b;
};

class B {
A a;
};

int main(int argc, char **argv)
{
return 0;
}

and it's perfectly legit C++ code. If that doesn't compile using your compiler then it's probably your compiler. Try upgrading. If you're using VC++ like me, try getting a VC++ service pack (SP5 is the latest).

Hope that helps.

Deiussum
06-27-2001, 05:48 AM
If you'd like me to take a look at your code, feel free to e-mail it to me. I think I already might see where you could be confused from looking at your code, though.




includes.h:
#ifndef __file1__
#include "file1.h"
#ifndef __file2__
#include "file2.h"
//if this part is not after the files(-includes) that
use it it will produce errors
#ifndef __fileimp__
#include "fileimportant.h"
//
file1:
#define __file1__
#include "includes.h"

struct yyy{
abc here;
};

file1: // I assume you mean file2 here?
#define __file2__
#include "includes.h"

struct xxx{
abc hereto;
yyy file1;//this should produce an error, because file 2 is after file 1 in includes.h
};

fileimportant.h:
#define __fileimp__
#include "includes.h"
struct abc{

};


Now... assume that in your .cpp file you do

#include "file1.h"

That will first include "includes.h", which will in turn include "file2.h", which will then include "includes.h" again, wich this time will include "fileimportant.h". SO! You now have

fileimportant.h
remainder of file2.h
remainder of file1.h

Which translates to




struct abc{

};

struct xxx{
abc hereto;
yyy file1
};

struct yyy{
abc here;
};


Which is obviously wrong.

Likewise if you #include "file2.h" first the order becomes

fileimportant.h
remainder of file1.h
remainder of file2.h

So you get



struct abc{

};

struct yyy{
abc here;
};

struct xxx{
abc hereto;
yyy file1
};



Which would be right.

Lastly if you #include "fileimportant.h" you get

file2.h
remainder of file1.h
remainder of fileimportant.h

or



struct xxx{
abc hereto;
yyy file1
};

struct yyy{
abc here;
};

struct abc{

};


Which still is not right.

As you can see, trying to do this with precompiler stuff the way you did makes things needlessly messy and didn't even fix anything. And actually, in that example you don't even have circular dependencies. All you probably have to do is at the top of one of your two headers pre-declare the other class.


Got confused myself while evaluating the headers and fixed them above. http://www.opengl.org/discussion_boards/ubb/smile.gif


[This message has been edited by Deiussum (edited 06-27-2001).]

T2k
06-27-2001, 11:21 PM
hmm, its a bit confusing, but i think i have understod how it work, and i will try to find an easier method to solve this, but at the moment there isnt any possible way ...