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

问题描述 投票:0回答:2

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

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();
}
c++ templates
2个回答
49
投票

应该是:

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

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


7
投票

用途:

template<class T>
bool MutableQueue<T>::empty() const
{
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.