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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13477086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:21:55  来源:igfitidea点击:

use of class template requires template argument list

c++templates

提问by Torrius

I moved out the methods implementation from my class and caught the following error:

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

use of class template requires template argument list

for method whitch doesn't require template type at all... (for other methods all ok)

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

Class

班级

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

Wrong implementation

错误的实施

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

回答by xiaoyi

It should be:

它应该是:

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

And if your code is that short, just inline it, as you can't separate the implementation and header of a template class anyway.

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

回答by stativ

Use:

用:

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