Page 2 of 2

Posted: Sun Sep 25, 2005 4:04 pm
by campino
I'm not sure if you know what the problem is now, so, if you no, ignore my post, if you don't read it ;)

The compiler goes through the code line by line. So when he found an

Code: Select all

class something;
he knows: there is a class named something. So he can build an instance of it:

Code: Select all

int main(){
something st=new something();
}
but he didn't know which methods are in there. So he throw an error every time any method is called, also if the method is defined later. Hope that was more clear and necessary.

Posted: Sun Sep 25, 2005 4:35 pm
by Guest
so basically buddy, dont try to forward define your classes. a class is not a function or a variable. its different.

Posted: Mon Sep 26, 2005 10:53 am
by cpprules
it is possible, i know it. I saw it somewhere just can't remember where...

Posted: Mon Sep 26, 2005 11:48 am
by MindGames
In your 'bar' header file;

// forward declaration
class foo;

class bar
{
...
};

In your 'bar' cpp file
#include "foo"

void bar::fn()
{
foo f = new foo();
f->DoStuff();
}


In your 'foo' header file

class bar;

class foo
{
...
};

In your 'foo' cpp file

#include "bar"

void foo::DoSomething()
{
bar b = new bar();
b->DoStuff();
}

This will keep everything happy as when each class is used it has all the required information. If I were you though, I would question the design if this is so necessary. Obviously I cannot be certain without more context, but this situation smells of design issues.

Hope this helps

Posted: Mon Sep 26, 2005 12:44 pm
by Guest
why do you want to do this anyways?

Posted: Mon Sep 26, 2005 4:34 pm
by cpprules
I have many files. In each file theres at least one class. Each class is using each other's functions. If i include "class1" then "class2"; i can't use class2 from class 1.

Posted: Tue Sep 27, 2005 1:24 am
by Foole
Well that's a different story to just using it from your 'main' function.

There are 2 different types of forward declarations for classes.

Just putting "class myclass;" lets the compiler know that the class exists but doesnt say anything about its methods etc. This is usually used to resolve cyclic dependancy problems.

The other type is "class myclass { /* class member declarations here */ };" This lets the compiler know all the members of the class but doesn't have to define the body of each function. These normally go in header files.

Take a look in the irrlicht source to see how to mix these together. eg in IAnimatedMeshSceneNode.h, IAnimatedMeshSceneNode is forward declared so that IAnimationEndCallBack can use it. IAnimationEndCallBack can't call any of IAnimatedMeshSceneNode's methods because they haven't been declared yet. This doesn't matter because this is only the declaration of IAnimationEndCallBack and doesn't contain any executable code. (This isn't a great example because all the methods are virtual and therefore never contain any code).

If this doesn't help, I give up. I suggest you read some C++ books/tutorials. (Or switch to C# 8-) )

Posted: Tue Sep 27, 2005 12:51 pm
by MindGames
So are you still having problems? The mechanism I posted will work. If you are still having problems I can try and reword the example a bit.

Posted: Tue Sep 27, 2005 1:06 pm
by cpprules
it works, thanks.
I forgot to add [solved] to the topic ( adding it right now ).

trying to understand the question

Posted: Tue Sep 27, 2005 1:15 pm
by Guest
if you want to define AND declare the class, you already know how to :
class myClass
{
myClass()
{
// implement the code in here
}
otherMethods()
{
// etc.
}
};

but doing this, you cannot easily make it visible from the others, because the compiler won't allow you to include such a thing more than once = declare a class more than once.

About the predecence / forward declaration, i advise you to have a quick look at this (short & clear)
http://www.parashift.com/c++-faq-lite/m ... #faq-39.11

this C++ FAQ exists in several languages if you prefer sth else than english