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

Tor*_*ius 21 c++ templates

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

use of class template requires template argument list
Run Code Online (Sandbox Code Playgroud)

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

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;
};
Run Code Online (Sandbox Code Playgroud)

错误的实施

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

xia*_*oyi 41

它应该是:

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

如果您的代码很短,只需内联它,因为您无法分离模板类的实现和标头.


sta*_*tiv 6

使用:

template<class T>
bool MutableQueue<T>::empty() const
{
    ...
}
Run Code Online (Sandbox Code Playgroud)