problem with child

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
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

problem with child

Post by Serg88 »

Hi all, im seems to have forgotten C++

My game has been blocked

I have base Class

Code: Select all

class gameModel
	{
	protected:
		u32 id;
		IrrlichtDevice* device;
		IAnimatedMesh* mesh;
		IAnimatedMeshSceneNode* node;

	public:

		gameModel(IrrlichtDevice *device);
		bool LoadModel(string s_mesh, core::vector3df obj_pos, core::vector3df obj_rotate, core::vector3df obj_scale);
		void setCollisionObject(IAnimatedMeshSceneNode* object);

//  some code ...
};

/***********************************************************/

// i load model here

bool gameModel::LoadModel(string s_mesh, core::vector3df obj_pos, core::vector3df obj_rotate, core::vector3df obj_scale)
{
	video::IVideoDriver* driver = this->device->getVideoDriver();
	scene::ISceneManager* smgr = this->device->getSceneManager();

	this->mesh = smgr->getMesh(s_mesh.c_str());
	if (!this->mesh)
		{
		device->drop();
		return false;
		}

	this->node = smgr->addAnimatedMeshSceneNode(this->mesh); // set node

		if (this->node)
		{
		//scene::ICameraSceneNode* camera = smgr->getActiveCamera();

			this->node->setPosition(obj_pos);
			this->node->setRotation(obj_rotate);
			this->node->setScale(obj_scale);

			//this->node->setMaterialFlag(video::EMF_LIGHTING, false);//global light for example, here this parametr work, but in child`s ...
		}

return true;
}

/***********************************************************/

// and child class, 

class goMarker : public gameModel
	{
	protected:
		u32 id;
		IrrlichtDevice* device;
		IAnimatedMesh* mesh;
		IAnimatedMeshSceneNode* node;
	public:

		goMarker(IrrlichtDevice *device, IAnimatedMeshSceneNode* object, core::vector3df obj_pos, core::vector3df obj_rotate): gameModel(device) // parent`s constructor
		{
			this->mesh = 0;
			this->node = 0;
			this->LoadModel("obj/items/piramid1.3DS", obj_pos, obj_rotate, core::vector3df(1.0,1.0,1.0));
			//if(this->node)
				{ // this block do not work, because this->node == NULL 
				  //every time my programm terminate
				this->node->setMaterialFlag(video::EMF_LIGHTING, false);
				}
		}

		bool LoadModel(string s_mesh, core::vector3df obj_pos, core::vector3df obj_rotate, core::vector3df obj_scale)	{return gameModel::LoadModel(s_mesh,obj_pos,obj_rotate,obj_scale);}
// some code...
};

/***********************************************************/

//in init block i load my model

gameModel* pir1 = new goMarker(device,this->player.node,core::vector3df(5400,980,2800),core::vector3df(0,0,0));
from Russia with love :))))
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Code: Select all

   protected: 
      u32 id; 
      IrrlichtDevice* device; 
      IAnimatedMesh* mesh; 
      IAnimatedMeshSceneNode* node; 

is not needed in the child class as it already exists in the parent class......
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

Post by Serg88 »

Great thanks!!! i`ts worked :)))))

Can you tell me some advanced books about C++ OOP, with detailed description of constructor and initialization list - in which cases its used?

I am also interested in a virtual function, I can not find good books :(((

I have a year lost all my knowledge :(
from Russia with love :))))
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

Linking error

Post by Serg88 »

I need help, i can`t anything understand.

i use in each .h file
#ifndef FILE_NAME_H
#define FILE_NAME_H

then my code

but when im define messenger class, for messages queue
i see this:

Code: Select all

Linking...
obj_items.obj : error LNK2005: "class MY_Messenger __msg" (?__msg@@3VMY_Messenger@@A) already defined in game_levels.obj
start_engine.obj : error LNK2005: "class MY_Messenger __msg" (?__msg@@3VMY_Messenger@@A) already defined in game_levels.obj
world_object.obj : error LNK2005: "class MY_Messenger __msg" (?__msg@@3VMY_Messenger@@A) already defined in game_levels.obj
C:\Documents and Settings\Spirit\Рабочий стол\demo_01\Release\demo_01.exe : fatal error LNK1169: one or more multiply defined symbols found
Im try change class and object name, and write "extern MY_Messenger __msg111;"
but it still fail.

it`s my class definition

Code: Select all

#ifndef WORLD_OBJECT_H
#define WORLD_OBJECT_H

// ...  some code

class MY_Messenger
{
private:
	IrrlichtDevice *device;
	gui::IGUIEnvironment* env;
	list<wchar_t* > Messages;

public:

	void SetDevice(IrrlichtDevice *device)
	{
		this->device = device; 
		this->env = device->getGUIEnvironment();
	}
	
	void addMessage(wchar_t* message)
	{
		this->Messages.push_back(message);
	}

	void ShowMessage()
	{
		env->clear();
		list<wchar_t*>::iterator msg_it = this->Messages.begin();
		env->addWindow( core::rect<s32>(100, 100 , 300, 200), false, *msg_it);
		this->Messages.pop_front();
	}
} __msg;

#endif
In game_levels.h and game_levels.cpp a similar definition or same named objects is absent
also i check global namespace
from Russia with love :))))
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The macro guards prevent the same file from being included multiple times. What you're doing there is right.

The problem is that you're declaring and defining a global variable in your header file. Every object file that includes this header will get a definition for __msg, and then when the linker does its thing, it will see multiple definitions for that symbol.

You shouldn't define global variables in your headers. Well, really you should avoid using globals at all because of the initialization issues, but that is an entirely different topic. If you must declare the object as a global, you can declare it in the header and define it in a source file...

Code: Select all

// world_object.h
class MY_Messenger
{
  // ...
};

extern MY_Messenger __msg;

Code: Select all

// world_object.cpp
#include <world_object.h>

MY_Messenger __msg;
Also, you should avoid using variable names that start with two underscores, or an underscore and then a capital letter. Those names are reserved for the implementation of the compiler and its libraries and can cause conflicts.

Travis
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

Post by Serg88 »

great thanks for the quick response and interpretation :)
from Russia with love :))))
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

again childs

Post by Serg88 »

Now i have error again :))))

Code: Select all

class IGameObjectNode : public IAnimatedMeshSceneNode
	{
	public:

		IGameObjectNode* operator=(IAnimatedMeshSceneNode* node) [b]// it`s not helped :([/b]
		{
			IAnimatedMeshSceneNode* anim =  const_cast<IAnimatedMeshSceneNode*>(node);
			return dynamic_cast<IGameObjectNode*>(anim);
		}

		IGameObjectNode* operator=(IGameObjectNode* node)
		{
			this = node;
		}

Code: Select all

bool irr::scene::IGameObjectNode::LoadModel()
{
// ... some code
this = smgr->addAnimatedMeshSceneNode(mesh); // cant convert
//  .\world_object.cpp(188) : error C2440: '=' : cannot convert from 'irr::scene::IAnimatedMeshSceneNode *' to 'irr::scene::IGameObjectNode *const '
//        Cast from base to derived requires dynamic_cast or static_cast
from Russia with love :))))
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It is not legal to assign to this. Your code is also pretty weird.

Code: Select all

bool IGameObjectNode::LoadModel() 
{ 
  this = smgr->addAnimatedMeshSceneNode(mesh); // cant convert 
You are assigning a IAnimatedMeshSceneNode* to a IGameObjectNode*. There is no logical reason to do this. I don't really know what you are trying to do, but I'm fairly certain you're going about it all wrong.

Answer this question for yourself, and then think about your design. Is a game object a animated mesh, or does a game object have an animated mesh. I think that will simplify things for you quite a bit.

Travis
Kalango
Posts: 157
Joined: Thu Apr 26, 2007 12:46 am

Post by Kalango »

problem with child
Call the supernany....
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Kalango wrote:
problem with child
Call the supernany....
Badoom tish! :lol:
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

Post by Serg88 »

vitek wrote:It is not legal to assign to this. Your code is also pretty weird.

Code: Select all

bool IGameObjectNode::LoadModel() 
{ 
  this = smgr->addAnimatedMeshSceneNode(mesh); // cant convert 
You are assigning a IAnimatedMeshSceneNode* to a IGameObjectNode*. There is no logical reason to do this. I don't really know what you are trying to do, but I'm fairly certain you're going about it all wrong.

Answer this question for yourself, and then think about your design. Is a game object a animated mesh, or does a game object have an animated mesh. I think that will simplify things for you quite a bit.

Travis
I want to extend class and add my own methods for convenient way to load models from the archives and also get rid of array of objects(i use vector<T*>) to use selector
because I am afraid that larger count of objects in array will take more time than just checking by selector.
Also i want connect PhysX to this class.
Last edited by Serg88 on Thu Oct 29, 2009 5:11 am, edited 1 time in total.
from Russia with love :))))
Serg88
Posts: 30
Joined: Mon Oct 19, 2009 5:52 pm
Location: Moscow/Russia

Post by Serg88 »

Kalango wrote:
problem with child
Call the supernany....
good idea :)))
from Russia with love :))))
Post Reply