Jump to content

  • Log In with Google      Sign In   
  • Create Account


I tried to do a simple factory but ended up with a wierd error?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
25 replies to this topic

#1 Conny14156   Members   -  Reputation: 258

Like
1Likes
Like

Posted 27 December 2012 - 10:43 PM

Hi,

I tried to do a basic hopefully almost correct factory where its simply just return new "insert class here".

Here the class I want to dublicate/create with my factory

class Enity
{
public:	
        Enity(){};
	std::vector<BaseComponent> ComponentList;
	template<class ComponentType>
	void AddComponent(ComponentType type);
	void Destroy();
};

 

Here the factory class, which Sit in a header btw

class BasicFactory
{
public:
	BasicFactory(){}
	virtual ~BasicFactory(){}

	template<class EnityType>
	EnityType* InstantiateEnity(EnityType type);
};
class EnityFactory : public BasicFactory
{
public:
	EnityFactory(){}
	virtual ~EnityFactory(){}
	template<class EnityType>
	Enity* InstantiateEnity(EnityType type);

};

 

And here I declared the function to the factory

 


template<class EnityType>
Enity* EnityFactory:: InstantiateEnity(EnityType type)
{
	return new type;
}

Seems like even a Idiot guide is to advance for me :(

http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1

and here's the tutorial I kinda followed

 

 

Here what I tried to do

Enity TestComp;
std::vector<Enity> vEnity;
std::vector<Enity>::iterator vEnityEnd;
int vEnityMax = 10;

EnityFactory eFactory;
int vEnitySize = 0;





void Update()
{
	vEnitySize = vEnity.size();
	vEnityEnd = vEnity.end();
	if(vEnitySize < vEnityMax)
	{
		//MessageBox(0,"Lets hope this work","Enity have NOT Increased by 1 yet!",MB_OK);
TestComp = &eFactory.InstantiateEnity(Enity); <- error of illegal use of this type as an expression
		vEnity.insert(vEnityEnd,TestComp);
		MessageBox(0,"Screw This","Enity Increased by 1!",MB_OK);
	}

	Test.Update(pWCamera);
	TestB.Update(pWCamera);
}

 

 

 



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12379

Like
1Likes
Like

Posted 27 December 2012 - 11:17 PM


  std::vector<BaseComponent> ComponentList;



I stopped reading here. You seem to be trying to have a container of elements of a base class, and then get polymorphic behavior on them. Well, that just can't be done. You need to have a container of pointers of some sort. std::vector<std::unique_ptr<BaseComponent>> is a good start, if you are using C++11.

Also, you seem to be missing a "t" in "Entity".

#3 Servant of the Lord   Crossbones+   -  Reputation: 17960

Like
3Likes
Like

Posted 27 December 2012 - 11:26 PM

As far as I can see, you didn't actually post your error message. wink.png

 

As far as I can guess, your error is telling you that your template isn't correct. Here's your template:

template<class EnityType>
Enity* EnityFactory:: InstantiateEnity(EnityType type)
{
     return new type;
}

 

As far as I can throw an exception (Poor attempt at humor. It's late), here's what I think is wrong:

 

First, you return a type of 'Entity'. I think you meant, EntityType.

 

Second, 'new' creates a dynamic class of type 'Type', right? So 'new int' creates a new int. You can't do 'new 5', even though '5' is an instance of an int.

Your function doesn't create a new EntityType (a class type), it's trying to create a new instance of EntityType.

 

This is incorrect:

int myInt = 5;
new myInt;
//Or:
EntityType type;
new type;

 

This is correct:

new int;
//Or:
new EntityType;

 

Thus, this:

template<class EnityType>
Enity* EnityFactory:: InstantiateEnity(EnityType type)
{
     return new type;
}

 

Should be this:

template<class EnityType>
EnityType* EnityFactory:: InstantiateEnity(EnityType type)
{
     return new EnityType;
}

 

And since you aren't using the parameter at all, you might as well remove it:

template<class EnityType>
EnityType* EnityFactory:: InstantiateEnity()
{
     return new EnityType;
}
I didn't explain that too well, but did you understand what I mean? You can't 'new' an instance of a type, you can only new a type itself.
 
 
Edit: Now I see your error message!
 
Using the corrected code I show above, the correct way to call such a function would be:
Entity *myEntity = eFactory.InstantiateEnity<Entity>();
 
By the way, the definition of your entire templated function must be in the header file, as ugly as that is. It's a shortcoming of templates. Any function that is templated must be entirely (declaration and definition) in the header. sad.png

Edited by Servant of the Lord, 27 December 2012 - 11:33 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#4 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 12:31 AM

Thanks you guys for the help :3 
I will try to fix both of the problems.



#5 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 12:56 AM

Sorry for asking again, I did the change as you mention Lord, but now it give me the error

error C2276: * : illegal operation on bound member function expression. 



#6 hannesnisula   Members   -  Reputation: 988

Like
0Likes
Like

Posted 28 December 2012 - 03:58 AM

At the line you said the error is you are trying to assign an Entity the address of a method that returns a pointer to an Entity. I guess you would want to assign an Entity pointer (Entity *) the Entity pointer that is returned by the InstantiateEntity() method.



#7 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 28 December 2012 - 04:16 AM

Hard to say without seeing the code. You probably missed some brackets. More fundamentally, factories are used when you need to create an object, but you don't know the type. Here you do know the type, and there is actually only one. If you did need a factory, it would not be templated like that because that implies you know the type. Try it without the factory, or at least without the templates. If you just want to copy an object, use the copy constructor.

#8 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 06:20 AM

>.<
I know why it gave me the * Error operator, I kinda failed to read correct (Was in a Hurry for my driving lesson) either way. tEntity is now a proper pointer and not a instance of Entity.

But May I ask
is this the correct way to insert something into a vector while am here I may aswell ask, instead of the * operator error I now get a error/warning on my template factory.
And the reason why I use template instead of pre defined classes is that I hope one day I will be good enough to make it more "dynamic" or more automatic instead of needing me to make a new factory for each new class.

std::vector vEntity;std::vector::iterator vEntityEnd;
int vEntityMax = 10;Entity* tEntity;
EntityFactory eFactory;
int vEntitySize = 0;
vEntitySize = vEntity.size();
vEntityEnd = vEntity.end();
if(vEntitySize < vEntityMax)
{
//MessageBox(0,"Lets hope this work","Entity have NOT Increased by 1 yet!",MB_OK); 
tEntity = eFactory.EntityInstantiate<Entity>();
vEntity.insert(vEntityEnd,tEntity);
MessageBox(0,"Screw This","Entity Increased by 1!",MB_OK);	
}


Edit:
Here's the error

 

 
</p><div>   Reason: cannot convert from 'Entity *' to 'const Entity'</div>
<div>1>          No constructor could take the source type, or constructor overload resolution was ambiguous</div>
<div>1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<Entity*&>(Entity *,_Other)' being compiled</div>
<div>1>          with</div>
<div>1>          [</div>
<div>1>              _Ty=Entity,</div>
<div>1>              _Other=Entity *&</div>
<div>1>          ]</div>
<div>1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(668) : see reference to function template instantiation 'void std::_Cons_val<std::allocator<_Ty>,Entity,Entity*&>(_Alloc &,_Ty1 *,_Ty2)' being compiled</div>
<div>1>          with</div>
<div>1>          [</div>
<div>1>              _Ty=Entity,</div>
<div>1>              _Alloc=std::allocator<Entity>,</div>
<div>1>              _Ty1=Entity,</div>
<div>1>              _Ty2=Entity *&</div>
<div>1>          ]</div>
<div>1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(688) : see reference to function template instantiation 'void std::vector<_Ty>::emplace_back<Entity*&>(_Valty)' being compiled</div>
<div>1>          with</div>
<div>1>          [</div>
<div>1>              _Ty=Entity,</div>
<div>1>              _Valty=Entity *&</div>
<div>1>          ]</div>
<div>1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(675) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::emplace<Entity*&>(std::_Vector_const_iterator<_Myvec>,_Valty)' being compiled</div>
<div>1>          with</div>
<div>1>          [</div>
<div>1>              _Myvec=std::_Vector_val<Entity,std::allocator<Entity>>,</div>
<div>1>              _Ty=Entity,</div>
<div>1>              _Valty=Entity *&</div>
<div>1>          ]</div>
<div>1>          c:\users\conny.conny-dator\desktop\fusionempty\fusionempty\windowshandler.cpp(223) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::insert<Entity*&>(std::_Vector_const_iterator<_Myvec>,_Valty)' being compiled</div>
<div>1>          with</div>
<div>1>          [</div>
<div>1>              _Myvec=std::_Vector_val<Entity,std::allocator<Entity>>,</div>
<div>1>              _Ty=Entity,</div>
<div>1>              _Valty=Entity *&</div>
<div>1>          ]</div>
<div>
 

Edited by Michael Tanczos, 29 December 2012 - 12:40 AM.


#9 Servant of the Lord   Crossbones+   -  Reputation: 17960

Like
2Likes
Like

Posted 28 December 2012 - 09:50 AM

Vectors can't be declared "std::vector myVector;", a vector itself is a template, and must be declared like this:
std::vector<int> myVector; //If it's a vector of 'ints'
 
So yours would be:
std::vector<Entity> myVector; //Vector of Entities.
 
But polymorphism requires you to carry your entities in either a pointer or a reference.
std::vector<Entity*> myVector; //Vector of Entity pointers.
 
If you are inserting pointers into a vector, you do this:
Entity *myEntity = ...;
myVector.push_back(myEntity); //Adds it to the end of the vector.
Because the entity is already in pointer form, and the vector takes pointers, you pass it directly into it. No & or * or anything else.
 
Since you are creating the entities with 'new' you must delete them with 'delete' - and vectors won't automatically delete your dynamically allocated memory for you. This can get annoying.
//Loop over every element in the vector.
for(size_t i = 0; i < myVector.size(); i++)
{
     delete myVector[i]; //Unallocate the memory for this entity.
}
myVector.clear(); //Resize the vector to 0.
 
 
//In C++11 standard, we'd use:
for(Entity *entity : myVector)
{
     delete entity;
}
myVector.clear();
 
Vectors call the destructor of every object in it when the vector is destroyed. Pointers don't have destructors, and don't destruct the thing they point to (because what if two pointers are pointing at the same memory? Neither pointer knows about the other).
The vector destroys the pointers, but not the memory they point to.
 
Instead, if we want the destruction of the pointer to automatically clean up the memory (which would be cleaner and safer), we need to use 'smart pointers'.
Smart pointers are an idea for pointers that destroy what they point to, only if no other pointer is still using the memory.
 
In C++11 (if you have it available), you have std::unique_ptr, std::shared_ptr, and std::weak_ptr. You also have the defective std::auto_ptr (even before C++11), but don't use that, it's been deprecated for good reason.
 
So, you can have a vector of smart pointers. When the vector is destroyed, it destroys the smart pointers, and the smart pointers destroy the objects they point to.
 
std::vector<std::shared_ptr<Entity>> myEntities;
 
 
Now your template function needs to return a smart pointer instead of a regular pointer:
 
template<class EntityType>
std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity()
{
     return std::make_shared<EntityType>();
}
 
std::make_shared<Type>() creates a std::shared_ptr pointing to a instance of 'Type' that std::make_shared dynamically allocates for you (so no need to call 'new'). Zero or more arguments can be passed to std::make_shared, and are handed to Type's constructor.

Edited by Servant of the Lord, 28 December 2012 - 09:52 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#10 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 05:25 PM

Thanks again for another awesomeness explanation, 

P.S

Pointers everywhere!

Miss the simplicity of c# :(

 

 

 

Performance question

If this vector that hold my pointers were to be "in game" enemies, would it be better to NOT delete it and simply "re use" it, after the "death timer"/"spawn timer" have been reach?. And is there a good website somewhere that would explain these types of detail? If I gonna start code in c++ I may aswell go right from the beginning >.<. oh would also be nice if anyone could give me a good place reading on about the 

smart pointers seeing they seems to be good thing to know about. Untill then may my google-fu help me XD.

 

P.S

Everything works now! thanks alot



#11 Servant of the Lord   Crossbones+   -  Reputation: 17960

Like
0Likes
Like

Posted 28 December 2012 - 08:54 PM

Performance question
If this vector that hold my pointers were to be "in game" enemies, would it be better to NOT delete it and simply "re use" it, after the "death timer"/"spawn timer" have been reach?

Well, first, don't try to optimize before it's necessary, or you risk accidentally optimizing all the wrong parts of the code.

But in answer to your question, yes it would be better to not delete it *if* you allocate/deallocate tens of thousands of enemies every frame. Even a few hundred won't hurt anything. Only make such optimizations when you actually encounter a slowdown and after you identify what is causing the slowdown using a profiler.
 

And is there a good website somewhere that would explain these types of detail? ... oh would also be nice if anyone could give me a good place reading on about the smart pointers seeing they seems to be good thing to know about.

 
There's no one real website that really covers everything about C++ perfectly, though I do personally find Parashift C++ FAQ very incredibly useful.

For smart pointers, C++'s smart pointers were mostly ported from the Boost C++ libraries, so until good tutorials and documentation arrives for the std::shared_ptr and such, you can read the documentation and tips for boost::shared_ptr.

Boost smart pointer documentation (especially the frequently asked questions)

I wrote a GameDev.net post about smart pointers here

Here's an important performance tip about smart pointers


 

Everything works now! thanks alot

 
Glad to hear it! There is one other optional addition you might want, but I didn't want to suggest it while you were still working through the earlier errors.
 
 

template<class EntityType, typename... Args>
std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity(Args &&... args)
{
     return std::make_shared<EntityType>(args...);
}

 
This lets you pass as many parameters as you want to InstantiateEntity(), and InstantiateEntity() will pass them to std::make_shared() (which internally passes them to EntityType's constructor).
 
The && symbol is called an r-value reference, and lets you get a reference to variables that might be unnamed temporary variables, in the same way & lets you get a reference to named variables. The '...' means we're getting an unknown number of variables.
 
Putting them together, we get an unknown number of possibly (or possibly not) unnamed temporary variables that we then pass to the class you are constructing, so you can use its constructor parameters even when creating through InstantiateEntity().
 
Then, instead of just passing the values to std::make_shared, what we actually want is to 'perfectly forward' the values, so that it is as if we passed the variables directly to the constructor itself, instead of going through several functions first.
 

template<class EntityType, typename... Args>
std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity(Args &&... args)
{
     return std::make_shared<EntityType>(std::forward<Args>(args)...);
}


It makes sure that if we passed any parameters by value, they won't get accidentally passed by value a second time, but instead as a r-value reference.
 
You can read more of r-value references and std::forward<> here. Page 7 talks exactly about the circumstances you are encountering.


Edited by Servant of the Lord, 28 December 2012 - 09:03 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#12 LordJulian   Members   -  Reputation: 151

Like
0Likes
Like

Posted 28 December 2012 - 09:30 PM

OFFTOPIC, straight ahead!

Miss the simplicity of c# sad.png

 

AHAHA! I thought my sharp-jitsu senses were tingling from the 1st implementation. In c# indeed, the behavior is inferred from the type being a value-type or a reference type  (and from the fact that the garbage collector sweeps all the time behind your back).

 

Now, in C++ you have to do all the hand-holding yourself for all your class instances (and if you want to share them around via pointers, you're gonna have a ... good... time ).

The thing is, try to keep it simple and "make up" rules for yourself, like "whoever requests a pointer must also tell me when they don't need it anymore"  or " I made you, I'll destroy you" i.e. "who calls new will also call delete".

 

Also, when learning C++, since you have all the time in the world (yea, right...) my personal advice, and some people will certainly disagree, is to try to do it only with pure c/c++ features, not with all kinds of libraries (boost, etc etc ). It helps separate the language learning from everything else and, AFTER you learn to do it in the pure language variant, you will have a better experience in identifying the pros and cons of each library feature and will choose them wisely. Again, my very own personal opinion is (and I am allowed to have one, dear internet :) ) that using only language features is beneficial for learning and libraries are for productivity.

 

Have fun!



#13 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 10:10 PM


Have fun!
Yeha sure >.>

I will try to remember you tips :3 and make some more rules for myself.

while am here I may aswell ask
I literately copy pasted my Entityfactory code to do something like factory handler, but instead of returning a smart pointer towards a Entity its points (well suppose to) points towards a "component" instead, than Its suppose to insert the component pointer inside the entity componentlist map, Isnt this suppose to be working or did I missunderstood horrible wrong?

std::shared_ptr<BaseComponent> pComponent;

pComponent = cFactory.ComponentInstantiate<TransformComponent>();
vEntity[0]->AddComponent(pComponent);

class FactoryComponent
{
public:
FactoryComponent(){}
virtual ~FactoryComponent(){}

template<class ComponentType>
std::shared_ptr<ComponentType> ComponentInstantiate()
{
return std::make_shared<ComponentType>();
}
};

class Entity
{
public:
Entity(){};

std::map<std::string,std::shared_ptr<BaseComponent>> ComponentList;

void AddComponent(std::shared_ptr<BaseComponent> type)
{
if(ComponentList.count(type->getTypeName()) == 0)
{
ComponentList.insert(std::make_pair(type->getTypeName(),type));
}
}

void Destroy();



};

Edited by Conny14156, 28 December 2012 - 10:30 PM.


#14 Servant of the Lord   Crossbones+   -  Reputation: 17960

Like
0Likes
Like

Posted 28 December 2012 - 10:24 PM

The code tags have been messing up. Here's a repost of your code, for others:

std::shared_ptr<BaseComponent> pComponent;
 
pComponent = cFactory.ComponentInstantiate<TransformComponent>();
vEntity[0]->AddComponent(pComponent);

class FactoryComponent
{
public:
	FactoryComponent(){}
	virtual ~FactoryComponent(){}
	
	template<class ComponentType>
	std::shared_ptr<ComponentType> ComponentInstantiate()
	{
		return std::make_shared<ComponentType>();
	}
};

class Entity
{
	public:
	Entity(){};
	
	std::map<std::string,std::shared_ptr<BaseComponent>> ComponentList;
	
	void AddComponent(std::shared_ptr<BaseComponent> type)
	{
		if(ComponentList.count(type->getTypeName()) == 0)
		{
			ComponentList.insert(std::make_pair(type->getTypeName(),type));
		}
	}
	
	void Destroy();
};

 
 
I'm not immediately seeing anything wrong. Is it failing to compile?


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#15 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 10:33 PM

Yeha It fail to compile :(

here the error code, it says the type cast is possible but not accessible? is that even possible? tbh am really confuse >.<

c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1238): error C2243: 'type cast' : conversion from 'TransformComponent *' to 'BaseComponent *' exists, but is inaccessible
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1536) : see reference to function template instantiation 'std::tr1::_Ptr_base<_Ty>::_Ptr_base<TransformComponent>(std::tr1::_Ptr_base<TransformComponent> &&)' being compiled
1>          with
1>          [
1>              _Ty=BaseComponent
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1565) : see reference to function template instantiation 'std::tr1::shared_ptr<_Ty>::shared_ptr<TransformComponent>(std::tr1::shared_ptr<TransformComponent> &&,void **)' being compiled
1>          with
1>          [
1>              _Ty=BaseComponent
1>          ]
1>          c:\users\conny.conny-dator\desktop\fusionempty\fusionempty\windowshandler.cpp(99) : see reference to function template instantiation 'std::tr1::shared_ptr<_Ty> &std::tr1::shared_ptr<_Ty>::operator =<TransformComponent>(std::tr1::shared_ptr<TransformComponent> &&)' being compiled
1>          with
1>          [
1>              _Ty=BaseComponent
1>          ]


#16 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 10:34 PM

Oh Forgot to include the component classes

Posting a new post, seeing how I fail misserbly when I try to edit

class BaseComponent
{
	
public:	
	virtual bool Initializing();	
	virtual void Update();
	virtual void Destroy();
	virtual std::string getTypeName();
};

	

class TransformComponent : BaseComponent
{
private:
	struct Transfromers
	{
		XMMATRIX Translation;
		XMMATRIX LocalPosition;
		XMMATRIX WorldPosition;
		XMMATRIX Rotation;
		XMMATRIX Scale;
	
	};


public:

	TransformComponent()
	{

		typeName = "TransfromComponent";
	}
	bool Initializing();
	bool Setting();
	void Update();
	void Destroy();
	std::string getTypeName();
	Transfromers Transform;
	std::string typeName; 
};


#17 Servant of the Lord   Crossbones+   -  Reputation: 17960

Like
0Likes
Like

Posted 28 December 2012 - 10:53 PM

I never encountered that error before! happy.png
 
Googling brought up this on the third hit. The first answer in that thread explains the error message.
 
Your code:
class TransformComponent : BaseComponent

...has 'TransformComponent' inherit 'BaseComponent', but didn't specify whether to inherit it publicly or privately. Private is default.

So 'TransformComponent' privately inherits 'BaseComponent', and since it privately inherits it, you can't convert the pointers.

The fix is to publicly inherit it:
class TransformComponent : public BaseComponent

Edited by Servant of the Lord, 28 December 2012 - 10:53 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#18 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 December 2012 - 11:11 PM

"facepalm" forgot I need to inherit the public functions >.<

C# I MISS YOU!

It works!

But dont worry I shall be back with more noobish misstake!



#19 Trienco   Crossbones+   -  Reputation: 2078

Like
0Likes
Like

Posted 28 December 2012 - 11:59 PM

I think you misunderstand the meaning of "public" when inheriting. It doesn't mean "inherit the public functions" it means "keep things public that are public in the base class" or "don't additionally limit visibility of inherited members".

 

One annoying thing about private inheritance is that your classes will seem "unrelated" to the outside.

 

class BaseException {};

class DerivedException : BaseException {}; //or private BaseException

try {}
catch (const BaseException& ) {}

 

 

You'd expect that when a DerivedException is thrown, it will be caught as well, but since "officially" DerivedException is _not_ a BaseException, it will just slip right through. Unlike in your situation, you will not be getting a compiler error either.

 

The same applies to overloaded functions, though it's unlikely you will ever create a situation where it won't just fail to compile.


f@dzhttp://festini.device-zero.de

#20 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 29 December 2012 - 01:54 AM

oh that's good to know about the public inheritance.

While am here >.< How do I know if my "compiler" is of c++11?

seeing how the foreach (well I think its foreach) that the servant of the lord showed as a example doesn't work am guessing Visual studio 2010 express is not c++11?

 

P.S Edit

How do I access a function(or a variable) inside a class that is inside a map that is inside a vector?

I thougth I could do it like this

 Test = vEntity[0]->ComponentList[0]->getTypeName();

But I get nothing in return o.o

when I say nothing it just returned " ", what it suppose to return is TransformComponent, And I checked the value before inserting it into the component list with the debug watcher, its does indeed have "TransformComponent" :( but how come I cant access it?
Did I fail with my polymorph again?

std::string Test;
std::vector<std::shared_ptr<Entity>> vEntity;


	vEntity.push_back(eFactory.EntityInstantiate<Entity>());
	vEntity[0]->AddComponent(cFactory.ComponentInstantiate<TransformComponent>());
	if(vEntity[0]->ComponentList.count("TransformComponent") == 1)
	{
		MessageBox(0,"It Works","Yay",MB_OK);
	}
	Test = vEntity[0]->ComponentList[0]->getTypeName();




class TransformComponent : public BaseComponent
{
private:
	struct Transfromers
	{
		XMMATRIX Translation;
		XMMATRIX LocalPosition;
		XMMATRIX WorldPosition;
		XMMATRIX Rotation;
		XMMATRIX Scale;
	
	};


public:

	TransformComponent()
	{
		typeName = "TransfromComponent";
	}
	bool Initializing();
	bool Setting();
	void Update();
	//void Destroy();
	std::string getTypeName();
	Transfromers Transform;
	std::string typeName; 
};


 

 


Edited by Conny14156, 29 December 2012 - 04:07 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS