2

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!

Casey
  • 41,449
  • 7
  • 95
  • 125
David
  • 21
  • 2
  • 1
    What are error are you getting? – Yochai Timmer Aug 06 '13 at 13:01
  • 4
    And btw, the template implementation must be in the header, not in the cpp file (unless of course it's a specialization that will be used only there). – Yochai Timmer Aug 06 '13 at 13:02
  • 2
    Don't forget the brackets: dynamic_cast(itr->second); – Bathsheba Aug 06 '13 at 13:03
  • thank you, but how do i put the template definition in the header that i dont have to create am templateclass but just a template for the function? and again do i have to cast to T or T*? i have added my thoughts in the question above, it would be very nice if someone could help me! – David Aug 06 '13 at 13:28
  • Without seeing the definition of `Baseclass`, the only problem in the code you've posted here is that it says `map.find` instead of `_map.find`, and that may just be a typo. When I tried this myself, I originally got error C2683 because I didn't put any virtual functions in `Baseclass`. But when I did that it compiled. What error are you getting? – Joel Aug 06 '13 at 13:38
  • when i put no additional definition of template in the cpp-file it says (i'm trying to translate it) "default int is not supported, it puts a red line under T* in the cpp file. i added a abstract method to Basclass but it still does not work. _map was a typo. the error that i get when i put another template definition in the cpp file is "unresolved reference to " the function of "someclass" in "file "where i wanted to call it. hope i could help! – David Aug 06 '13 at 13:49
  • With the `_map` typo corrected, [this code works correctly](http://coliru.stacked-crooked.com/view?id=20ce8f316a8a63279328004856cc58a3-e1204655eaff68246b392dc70c5a32c9). – Casey Aug 06 '13 at 17:14
  • Be aware that using containers of the Standard Library storing *owning* raw pointers is error prone. If you currently `delete` the pointers in `_map`, consider using smart pointers instead as the `value_type` of the map. – dyp Aug 06 '13 at 18:57

2 Answers2

0

I think some problems are occouring because of automatic conversions done by cpp compiler.

Try using template (left ang bracket) class mytype (right ang bracket)

Xperiaz X
  • 216
  • 1
  • 6
  • 16
-1

Try defining your template T as a class:

template <class T>

Without some solid error messages, I can't really help beyond that, it looks more or less ok to me at a glance.

Lochemage
  • 3,974
  • 11
  • 11
  • In most cases there is nothing different about the two, but that's not true in all cases. The compiler interprets them differently and in some cases it will cause a logic error when using one over the other. I'm not saying this is the problem he's having, I'm just suggesting that he try this to see if his error relates to this. – Lochemage Aug 06 '13 at 21:26