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

时间:2012-11-20 15:59:41

标签: c++ templates

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

use of class template requires template argument list

方法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;
};

错误的实施

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

2 个答案:

答案 0 :(得分:39)

应该是:

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

如果您的代码很短,那么只需内联它,因为无论如何都无法将模板类的实现和标题分开。

答案 1 :(得分:6)

使用:

template<class T>
bool MutableQueue<T>::empty() const
{
    ...
}
相关问题