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

阅读更多

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

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

 

原因:

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

 

例如:

template  
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& e){
        for (int i = 0; i < e.key.size(); i++) { out << e.key.at(i); }
        return out;
    }

private:
    vector key;
    vector object;
};

template  
KeyedCollection::KeyedCollection(){}

template 
int KeyedCollection::size() const { return key.size(); }

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

 

 

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