【问题标题】:class template requires template argument list类模板需要模板参数列表
【发布时间】:2014-01-25 20:10:14
【问题描述】:

我得到了错误-keyedcollection.h(34): error C2955: 'KeyedCollection' : use of class template requires template argument list

我已经搜索了几个小时的谷歌和其他网站,但仍然无法找到任何解决此问题的方法。关于我能做什么有什么建议吗?

声明:

  friend ostream& operator<<(ostream&, const KeyedCollection&);

定义:

  template <class K, class T> 
ostream& operator<<(ostream& out, const KeyedCollection& e){
    for (int i = 0; i < key.size(); i++){ out << key.at(i); }
    return out;
}

【问题讨论】:

  • 提示:模板参数列表是 中用于指定模板参数的东西。
  • 这就是我的想法,我在很多地方都尝试过。但我不确定把它放在哪里。我正在考虑争论,但我不确定在哪里。
  • K 和 T 用于什么用途?
  • 我尝试了 Lambda 告诉我的内容,但现在出现链接器错误:错误 LNK2019。关于如何解决这个问题的任何线索?
  • @user3236062 我修复了链接器错误。你想让我怎么给你密码?

标签: c++ templates


【解决方案1】:

操作符应该在类中。

template <class K, class T> 
class KeyedCollection {
public:
    // Create an empty collection
    KeyedCollection();

    // Return the number of objects in the collection
    int size() const;

    void get_vectorone();

    // Insert object of type T with a key of type K into the collection using an “ignore duplicates” policy
    void insert(const K&, const T&);

    // Output data value of objects in the collection, one data value per line
    friend ostream& operator<<(ostream& out, const KeyedCollection<K,T>& e){
        for (int i = 0; i < e.key.size(); i++) { out << e.key.at(i); }
        return out;
    }

private:
    vector<K> key;
    vector<T> object;
};

template <class K, class T> 
KeyedCollection<K,T>::KeyedCollection(){}

template <class K, class T>
int KeyedCollection<K,T>::size() const { return key.size(); }

template <class K, class T> 
void KeyedCollection<K,T>::insert(const K& id, const T& customer){
    key.push_back(id);
    object.push_back(customer);
}

【讨论】:

  • 好的,我试过了,但现在我收到了一个链接器错误:LNK2019 关于我将如何解决这个问题的任何线索?
  • 你能发布整个源代码吗?如果我看不到它很难提供帮助。
  • @user3236062 模板函数的定义必须在头文件中,不能在cpp文件中。
猜你喜欢
  • 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
相关资源
最近更新 更多