2014-01-25 55 views
1

我得到的错误 - 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; 
} 
+2

提示:模板参数列表是您在<>中用来指定模板参数的东西。 – PlasmaHH

+0

这就是我的想法,我在很多地方尝试过。但我不确定把它放在哪里。我在争论中想,但我不确定在哪里。 – user3236062

+0

对于什么K和T被使用? – dieram3

回答

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

好吧,我试过,但现在我得到一个链接器错误:LNK2019任何线索,我将如何解决这个问题? – user3236062

+0

你能发布整个源代码吗?如果我看不见它,它很难提供帮助。 –

+0

@ user3236062模板函数的定义必须位于标题中,而不是cpp文件中。 –

相关问题