类模板需要模板参数列表

时间:2014-01-25 20:10:14

标签: c++ templates

我得到了error- keyedcollection.h(34):错误C2955:'KeyedCollection':使用类模板需要模板参数列表

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

声明:

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

1 个答案:

答案 0 :(得分:3)

操作员应该在班级内。

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