【问题标题】:use of class template requires template argument list使用类模板需要模板参数列表
【发布时间】:2012-11-20 15:59:41
【问题描述】:

我从班级中移出方法实现并发现以下错误:

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

【问题讨论】:

  • 不相关,但queue&lt;T&gt; queue 的命名约定真的很奇怪...类型名称应该很容易与实例名称区分开来
  • 我会听从你的建议,但这不是根本原因

标签: c++ templates


【解决方案1】:

应该是:

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

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

【讨论】:

  • as you can't separate the implementation and header of a template class anyway Ahhhh.. +1
  • “因为无论如何你都无法将模板类的实现和标头分开”。也就是说,除非您使用预处理器指令 (#include MutableQueue.tpp) 在标头末尾包含实现
  • “因为无论如何你都不能分离模板类的实现和标头”——或者除非你明确地实例化它。
【解决方案2】:

用途:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多