Class Compile Errors

Started by
2 comments, last by AlexLoren 18 years, 8 months ago
I am fairly new to the C++ programming scene, so please do try and bear with me through this. I am attempting to write application and scene management classes for a small top-down space shooter, though when I compile the classes I am getting errors. While I am using the Irrlicht Device as a rendering engine, this is not an Irrlicht specific problem; or I do not believe so. If anyone can offer some sort of insight on this compile error, I would be extreamly greatful :)
Quote: alexl@linux:~/development/projects/irrlicht2d/src> make g++ main.cc -o irrlicht2d -I"../include" -I"/usr/X11R6/include" -L"/usr/X11R6/lib" -L"../lib/Linux" -lIrrlicht -lGL -lGLU -lXxf86vm -lXext -lX11 In file included from main.cc:15: system.h:23: error: ISO C++ forbids initialization of member `Game2D_Device' system.h:23: error: making `Game2D_Device' static system.h:23: error: invalid in-class initialization of static data member of non-integral type `irr::IrrlichtDevice*' system.h: In member function `int cGame2D::Create(irr::video::E_DRIVER_TYPE, int, int, int, bool)': system.h:45: error: `Game2D_Device' undeclared (first use this function) system.h:45: error: (Each undeclared identifier is reported only once for each function it appears in.) system.h:46: error: `Game2D_Deivce' undeclared (first use this function) system.h: At global scope: system.h:52: error: declaration of `void cGame2D::Destroy()' outside of class is not definition system.h:53: error: syntax error before `{' token In file included from main.cc:15: system.h:57:7: warning: no newline at end of file make: *** [all] Error 1
Quote: //system.h class cGame2D { protected: irr::IrrlichtDevice* Game2D_Device = 0; public: cGame2D(); ~cGame2D(); int Create(video::E_DRIVER_TYPE driver,int width,int height,int depth,bool screenMode); int setScene(const char* scene); void Destroy(); }; //--- int cGame2D::Create(video::E_DRIVER_TYPE driver,int width,int height,int depth,bool screenMode) { Game2D_Device = createDevice(driver,dimension2d<s32>(width,height),depth,screenMode,false,false,0); if (Game2D_Deivce == 0) { return false; } } void cGame2D::Destroy(); { Game2D_Device->drop(); }
Advertisement
This line isn't valid:

irr::IrrlichtDevice* Game2D_Device = 0;

Since it's a member variable, you have to initialize it in the constructor.
Variables are initialized in the constructor, not the class definition. Like this:

class Foo{public:    Foo();private:    int a;    float b;};Foo::Foo() : a(3), b(2.5){    ....}
Thank you both for the input on how to fix this error, The way I went abouts solving this problem was to change the class to the below code. Such a simple fix I can't believe I didn't catch it the first time I looked at it, just goes to show that I still have a long way left to go in my learning.

Quote:
NEW:
class cGame2D
{
private:
irr::IrrlichtDevice* Game2D_Device;
public:
cGame2D();
~cGame2D();
int Create(video::E_DRIVER_TYPE driver,int width,int height,int depth,bool screenMode);
int setScene(const char* scene);
void Destroy();
};

OLD:
class cGame2D
{
protected:
irr::IrrlichtDevice* Game2D_Device = 0;
public:
cGame2D();
~cGame2D();
int Create(video::E_DRIVER_TYPE driver,int width,int height,int depth,bool screenMode);
int setScene(const char* scene);
void Destroy();
};

This topic is closed to new replies.

Advertisement