i'm really struggeling with a problem in c++ that i'm really frustrated about:
the problem is that while i'm coding there are no errors, everything looks fine (i'm using Vs2012) but when i try to compile it there are many errors, depending how i'm varying the code.
i really can't get this to work and i hope that you could help me, this should be easy for an expert! this is basicly a bit of my code, all includes are finde and the project is setup properly:
class someclass //that stores the references
{
public:
// ..........
template <typename T>
T* getComponent(string name)
// headers and cpp files are actually seperated
{
auto itr = _map.find(name);
if (itr == _map.end())
return 0;
return dynamic_cast<T*>(itr->second);
}
private:
unordered_map<string, Baseclass*> _map;
}
the way i'm trying to call it is something like:
DerivedFromBase* d = someclass->getComponent<DerivedFromBase>("derived");
i dont know if i'm misunderstanding templates pretty bad or i'm just a minor step away from the solution that is why i'm posting my question here, i hope an expert my give me a hint. i was just trying to polish my code a little bit, without templates it looks like this (and it works):
class someclass
{
Base* getComponent(string) //...
};
and the way i call it is:
Derived* d = (Derived*) someclass->getComponent(name);
this actually works but i thought the concept with templates would be superior, but again, i don't know if i misunderstand it pretty bad. thank you in advance for any kind of help!
thank you for your hints. i really forgot the brackets here but they were in my code. btw sorry for my bad english i hope you can understand what i mean ;) do i have to cast to T or T* (the actual returntype or does the cast already give me a T* so that my cast in T* actually results in T*? i have the definition of template in both, my cpp and my headerfile,this could be the error, cause when i leave it in the c++-file it says something like "unknown type-specifier T", but if i put this on top of my .h-file my whole class is considered to be a templateclass (which is not what i want because i need several derived classes (lkie derived1, derived2* etc.. of different types). i hope this was somehow useful, thanks again for alle the effort!