class declaration [solved]
class declaration [solved]
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)
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)
Hater of Counter Strike (i hate it so much damn it)
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
}
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.
There are 10 sorts of people, those who understand binaries and those who don't.
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()
{
}
}
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)
Hater of Counter Strike (i hate it so much damn it)
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
Sys specs:
AMD 3700+ 64 processor
1.0 gb ram
e-Geforce 6600 graphics 256 mb onboard ram
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.
#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)
Hater of Counter Strike (i hate it so much damn it)
I do it like this:
Has anyone sucsseded in 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()
{
}
}
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Hater of Counter Strike (i hate it so much damn it)
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):Definition (someclass.cpp):Then your 'main' file becomes
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.
Declaration (someclass.h):
Code: Select all
class someclass
{
public:
void func();
}
Code: Select all
void someclass::func()
{
// code here
}
Code: Select all
#include "someclass.h"
int main()
{
someclass something;
something.func();
}
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.
No thats not it. I'l explain it very carefully. Think of a Class as an Int :
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:
All this is called forward declaration, Now how to do it for a class...
Code: Select all
int i;
void main()
{
cout<<i;
}
int i = 42;
Now think of a class as a function:
Code: Select all
void funtion();
int main()
{
funciton();
}
void function()
{
// do something
}
CRPG, FRPG, Oblivion Fan
Hater of Counter Strike (i hate it so much damn it)
Hater of Counter Strike (i hate it so much damn it)
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.
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.