Compilation error – defining a concrete implementation of templated abstract class

I have an abstract class in my header file:

template <class T>
    class IRepository {
    public:
        virtual bool SaveData(Result<T> &r) = 0;
        //virtual Result<T> & GetData()const = 0;
        virtual ~IRepository() {}
    };

I inherit it in the header file itself:

template <class T>
    class Repo1 :public IRepository<T>
    {
    public:
        bool SaveData(Result<T> &r)override;
        //Result<T> & GetData() const override;
    private:
        std::mutex mx;
        std::queue<T> m_q;
    };

I am trying to define SaveData in the cpp file:

template <class T>
    bool Repo1::SaveData(Result<T> &r)
    {
        std::lock_guard<std::mutex> lock(mx);
        if (m_q.size() > 10)
        {
            m_q.pop();
        }
        m_q.push(r);
        return true;
    }

The compiler complains:
‘twsensors::Repo1’: use of class template requires template argument list

The compiler doesn’t complain about the following:

template <class T>
    bool SaveData(Result<T> &r)
    {
        std::lock_guard<std::mutex> lock(mx);
        if (m_q.size() > 10)
        {
            m_q.pop();
        }
        m_q.push(r);
        return true;
    }

The problem with this is if I create class Repo2 :public IRepository<T>, then both of them will have their Save method point to the same definition.

What’s the reason behind the compilation error?

>Solution :

While implementing the template class method outside the class definition, the template class requires template arguments:

template <class T>
bool Repo1<T>::SaveData(Result<T> &r)

Leave a Reply