lundi 21 février 2022

How can I get access to typedef types or another way exist to avoid the error? [duplicate]

I study CRTP and write the code below, but it is not working. What happens and how can I resolve it? I know that I can make 3 parameters for Unit class, but I want to avoid it if it is possible. error C2504: 'Unit<Warrior<char,int>>': base class undefined

template<class SomeUnit>
class Unit
{
public:
    void foo(typename SomeUnit::Key key, typename SomeUnit::value_type v)
    {
        static_cast<SomeUnit*>(this)->foo(key, v);
    }
};

template<class T, class U>
class Warrior : public Unit<Warrior<T, U>>
{
public:
    typedef T Key;
    typedef U value_type;

    void foo(Key key, value_type v)
    {
        std::cout << key << " " << v << std::endl;
    }
};

int main()
{
    Unit<Warrior<char, int>> war;
    war.foo('a', 5);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire