class declaration [solved]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

Post 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.
Youth-Irrlicht-Coder

There are 10 sorts of people, those who understand binaries and those who don't.
Guest

Post by Guest »

so basically buddy, dont try to forward define your classes. a class is not a function or a variable. its different.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

it is possible, i know it. I saw it somewhere just can't remember where...
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post 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
Guest

Post by Guest »

why do you want to do this anyways?
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post 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.
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Foole
Posts: 87
Joined: Mon Aug 29, 2005 10:08 am

Post 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-) )
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post 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.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

it works, thanks.
I forgot to add [solved] to the topic ( adding it right now ).
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Guest

trying to understand the question

Post 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
Post Reply