使用类模板需要模板参数列表

use of class template requires template argument list

本文关键字:参数 列表      更新时间:2023-10-16

我从我的类中移出了方法实现并捕获了以下错误:

use of class template requires template argument list

对于方法,根本不需要模板类型...(对于其他方法都可以)

template<class T>
class MutableQueue
{
public:
    bool empty() const;
    const T& front() const;
    void push(const T& element);
    T pop();
private:
    queue<T> queue;
    mutable boost::mutex mutex;
    boost::condition condition;
};

错误的实现

template<>   //template<class T> also incorrect
bool MutableQueue::empty() const
{
    scoped_lock lock(mutex);
    return queue.empty();
}

它应该是:

template<class T>
bool MutableQueue<T>::empty() const
{
    scoped_lock lock(mutex);
    return queue.empty();
}

如果你的代码那么短,只需内联它,因为你无论如何都无法分离模板类的实现和标头。

使用:

template<class T>
bool MutableQueue<T>::empty() const
{
    ...
}