Static error

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.
Post Reply
Airslash
Posts: 17
Joined: Wed Jan 16, 2008 2:35 pm

Static error

Post by Airslash »

Eum, ok this might be a long post, but I'll try to keep it as short as possible...

Firs the error in the console during the compile :

Code: Select all

g++ -LC:\Users\Arne\workspace\bluedragoon\lib\win32 -obluedragoon.exe sound\src\soundengine.o render\src\renderengine.o render\src\loginmanager.o render\src\loginbuttonevent.o core\src\status.o core\src\skill.o core\src\characterfactory.o core\src\character.o core\src\battlemanager.o bin\main.o -lIrrlicht -lmingw32 -lSDL -lSDLmain -lSDL_mixer
render\src\loginbuttonevent.o: In function `ZN3irr4core12irrAllocatorIcE12internal_newEj':
C:/Users/Arne/workspace/bluedragoon/include/irrlicht/irrAllocator.h:(.bss+0x0): multiple definition of `render::LoginManager::_instance'
render\src\loginmanager.o:C:/Users/Arne/workspace/bluedragoon/include/irrlicht/irrAllocator.h:(.bss+0x0): first defined here
Build error occurred, build is stopped
Time consumed: 2911  ms. 
The mentioned file causing the problem:

Code: Select all

#ifndef LOGINMANAGER_H_
#define LOGINMANAGER_H_
#include <irrlicht.h>

namespace render {
	class LoginManager {
		private:
			irr::IrrlichtDevice* device;
			irr::video::IVideoDriver* video;
			irr::scene::ISceneManager* scene;
			static LoginManager* _instance;
			
		public:
			static LoginManager* instance();
			
			void createDevice(irr::IEventReceiver* reciever, irr::video::E_DRIVER_TYPE deviceType=irr::video::EDT_OPENGL, const irr::core::dimension2d<signed int>& windowsize=irr::core::dimension2d<signed int>(640, 480), unsigned int bitdepth=16, bool fullscreen=false, bool stencilbuffer=false, bool vsync=false);
			void render();
			
			irr::gui::IGUIEnvironment* getGUIEnvironment();
			
			void setEventReciever(irr::IEventReceiver* reciever);
		protected:
			LoginManager();
			~LoginManager();
	};
	
	// Initialze the pointer variable of the class.
	// Only the pointer, not the class.
	LoginManager* LoginManager::_instance = NULL;
}

#endif /*LOGINMANAGER_H_*/
and the implementation

Code: Select all

#include "../loginmanager.h"
using namespace render;

LoginManager::LoginManager() {
	this->device = 0;
	this->scene = 0;
	this->video = 0;
}

LoginManager* LoginManager::instance() {
	if(NULL == _instance) {
		_instance = new LoginManager();
	}
	
	return _instance;
}

irr::gui::IGUIEnvironment* LoginManager::getGUIEnvironment() {
	return this->device->getGUIEnvironment();
}

void LoginManager::setEventReciever(irr::IEventReceiver* reciever) {
	this->device->setEventReceiver(reciever);
}

void LoginManager::createDevice(irr::IEventReceiver* receiver, irr::video::E_DRIVER_TYPE deviceType, const irr::core::dimension2d<signed int>& windowsize, unsigned int bitdepth, bool fullscreen, bool stencilbuffer, bool vsync) {
	this->device = irr::createDevice(deviceType, windowsize, bitdepth, fullscreen, stencilbuffer, vsync, receiver);
	this->device->setWindowCaption((wchar_t*)"BlueDragoon Login");
	this->video = this->device->getVideoDriver();
	this->scene = this->device->getSceneManager();
}

void LoginManager::render() {
	// TODO: Implement render method
}

LoginManager::~LoginManager() {
	this->device->drop();
}

Could someone explain to me where it's going wrong with that static _instance call ?
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Code: Select all

   // Initialze the pointer variable of the class.
   // Only the pointer, not the class.
   LoginManager* LoginManager::_instance = NULL; 
This must be defined once and only once. Put it in the .cpp file rather than the header.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Airslash
Posts: 17
Joined: Wed Jan 16, 2008 2:35 pm

Post by Airslash »

but doesn't the #ifndef statement at the head of the header prevent double initialization ?

And I have three like these classes, but only this one is causing problems.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Airslash wrote:but doesn't the #ifndef statement at the head of the header prevent double initialization ?
No. It only stops inclusion more than once during compilation of each source file; it doesn't stop it being included by multiple source files and therefore defined multiple times at link time.

Airslash wrote:And I have three like these classes, but only this one is causing problems.
The others are only included by one source file.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Airslash
Posts: 17
Joined: Wed Jan 16, 2008 2:35 pm

Post by Airslash »

ok i'll make the changes then.
Thanks for pointing it out
Airslash
Posts: 17
Joined: Wed Jan 16, 2008 2:35 pm

Post by Airslash »

it works, thanks for the help
Post Reply