Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading