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

class declaration [solved]

Post by cpprules »

So here is the problem, When i want to acces functions that are after main() i use this:

int something(int a, float b);

void main()
{
something(2,3);
}

int something(int a, float b)
{
return a + b;
}

So how to do this for a class. Please help, i have tryed everything ( or is it possible at all)
Last edited by cpprules on Tue Sep 27, 2005 1:07 pm, edited 1 time in total.
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

Post by campino »

Code: Select all

class Someclass{
public: 
    int something(int a, int b);
};

int main(){
    Someclass sc=new Someclass();
    sc->something();
};

int Someclass::something(int a, int b){
    //your code
}
You can write the first block (starting with class ...) in an extra file (like Someclass.h) and include it in the begining.

Code: Select all

//File: Someclass.h
class Someclass{
public: 
    int something(int a, int b);
};

Code: Select all

//File main.cpp

#include "Someclass.h"

int main(){
    Someclass sc=new Someclass();
    sc->something();
};

int Someclass::something(int a, int b){
    //your code
}
Youth-Irrlicht-Coder

There are 10 sorts of people, those who understand binaries and those who don't.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

Thanks for the reply, but i allready knew that. What i want to know is how to acces whole class you know something like this:

class something()

int main()
{
something blah;
blah.doSomething();
}

class something
{
public:
void doSomething()
{
}
}
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

common, anyone...
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Guest

Post by Guest »

why dont you put your class into a .H file like everyone does and then include it? :?
MikeR
Posts: 767
Joined: Sun Dec 26, 2004 4:03 pm
Location: Northern California USA
Contact:

Post by MikeR »

Code: Select all

//main.cpp

#include your includes


class Someclass{ 
public: 
    int something(int a, int b); 
}; 

int main(){ 
    Someclass sc=new Someclass(); 
    sc->something(); 
}; 

int Someclass::something(int a, int b){ 
    //your code 
} 

int main(){ 
    Someclass sc=new Someclass(); 
    sc->something(); 
}; 

int Someclass::something(int a, int b){ 
    //your code 
} 
[code]
Should do it if that's what you are after.
If it exists in the real world, it can be created in 3d

Sys specs:
AMD 3700+ 64 processor
1.0 gb ram
e-Geforce 6600 graphics 256 mb onboard ram
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

Post by campino »

MikeR:
Why do you declare both functions twice?

cpprules:
You have to make a main-func outside the class wich calls the one in class.
Youth-Irrlicht-Coder

There are 10 sorts of people, those who understand binaries and those who don't.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

I have many header files, and in each flie theres a class. Each class uses other classes, and all classes are declared as external in main.cpp. The thing is if i have a class in file "1.h" and in "2.h", i include like this:
#include "1.h"
#include "2.h"
class in 2.h can use class in 1.h but it isn't working other way, MikeR I'l check that thing out.
And putting same class definiton twice, is like programming in QBasic.
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Guest

Post by Guest »

You mean forward declarations?

class MyClass;
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

Anonymous wrote:You mean forward declarations?

class MyClass;
Yes, Yes, Yes, Yes, Finaly i know how it's called.
So how to do it?
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

I do it like this:

Code: Select all

class someclass;

int main()
{
someclass something;
something.func();  // it doesn't works, compiler says that there is no func() in class
}

class someclass
{
public:
void func()
{
}
}
Has anyone sucsseded in this?
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 »

I would have thought this is fairly standard C++ stuff. You separate your class into declaration and definition. Declaration goes in the .h file and definition goes in the .cpp file.

Declaration (someclass.h):

Code: Select all

class someclass
{
public:
void func();
}
Definition (someclass.cpp):

Code: Select all

void someclass::func()
{ 
    // code here
}
Then your 'main' file becomes

Code: Select all

#include "someclass.h"

int main()
{
someclass something;
something.func();
}
Problem solved. A few other people (eg campino) wrote pretty much the same thing, but i've tried to explain it more.

If you REALLY insist on putting it all in one file, just replace the #include line with the class declaration and put the definition at the end of the file.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

No thats not it. I'l explain it very carefully. Think of a Class as an Int :

Code: Select all


int i;

void main()
{
 cout<<i;
}

int i = 42;

If everything succseds output would be "42"; ( Ofcourse this doesn't woks for an int, but its example )

Now think of a class as a function:

Code: Select all

void funtion();

int main()
{
  funciton();
}

void function()
{
// do something
}
All this is called forward declaration, Now how to do it for a class...
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 »

No, those are 2 very different things happening. With an int, you are declaring an instance of type int. You are later assigning a value to it.

With the function, you are declaring a function, then later defining it.

The error about the function missing from the class is because the class has not been declared. The "someclass something;" line in your code is declaring an instance of the class.

So, do you want to declare and define the class or do you want to declare an instance of the class?

On another note, this is one of the reasons I prefer C#. Forward declarations are not needed.
cpprules
Posts: 148
Joined: Wed Jul 27, 2005 8:37 pm
Location: on the Pedastal

Post by cpprules »

i want to declare and define a class.
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Post Reply