Help fix the source code, with a template

I tried to compile the example given ( C ++ Service Providers ) and was unable to execute VS8 VC9. I have little experience with the template.
Any suggestions?
Tanks.

These are errors:
dictionarystl.cpp (40): error C2663: 'std :: _ Tree <_Traits> :: find': 2 overloads have no legal conversion for the pointer 'this'
dictionarystl.cpp (48): error C2679: binary' [': no ​​operator found that accepts a right-hand operand of type' const type_info * __ w64 '(or no acceptable conversion)

#include <typeinfo>
#include <map>
#include <string>
using namespace std;

class SomeClass
{
public:
    virtual ~SomeClass() {} // virtual function to get a v-table
};

struct type_info_less
{
    bool operator() (const std::type_info* lhs, const std::type_info* rhs) const
    {
        return lhs->before(*rhs) != 0;
    }
};

class TypeMap
{
    typedef map <type_info *, void *, type_info_less> TypenameToObject;
    TypenameToObject ObjectMap;

public:
    template <typename T> 
    T *Get () const
    {
        TypenameToObject::const_iterator iType = ObjectMap.find(&typeid(T));
        if (iType == ObjectMap.end())
            return NULL;
        return reinterpret_cast<T *>(iType->second);
    }
    template <typename T> 
    void Set(T *value) 
    {
        ObjectMap[&typeid(T)] = reinterpret_cast<void *>(value);
    }
};

int main()
{
    TypeMap Services;
    Services.Set<SomeClass>(new SomeClass());
    SomeClass *x = Services.Get<SomeClass>();
}


      

+1


source to share


2 answers


To compile this code the following line:

typedef map<type_info *, void *, type_info_less> TypenameToObject;

      



it should be:

typedef map<const type_info *, void *, type_info_less> TypenameToObject;

      

+2


source


Change typedef

on line 33 to read:

typedef map <const type_info *, void *, type_info_less> TypenameToObject;

      



This will at least fix your second error. I haven't been able to reproduce your first error, but I suspect this will fix it too.

+1


source







All Articles