[C++] error C2955: 'XXXX' : use of class template requires template argumen

使用自定义模板类时,编译出现错误:

error C2955: 'XXXX' : use of class template requires template argumen

 

原因:

如果是模板类,则该类的所有函数(不管有没用到模版参数)的实现都必须放在头文件中。

 

例如:

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

 

 

你可能感兴趣的:(C++)