c++ - 类模板需要模板参数列表

标签 c++ templates

我收到错误 - 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;
}

最佳答案

运算符应该在类中。

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++ - 类模板需要模板参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355660/

相关文章:

java - 从数据库加载 FreeMarker 模板

c++ - 为什么模板运算符重载不起作用?

c++ - 带有 enable_if 和重载的 SFINAE

c++ - 谁能告诉我stream是c或c++中的保留关键字吗?

c++ - 为什么 bjam 似乎无视我的论点?

c++ - 为什么我必须通过this指针访问模板基类成员?

c++ - 从结构中获取所有可变参数模板类型,该结构是函数模板中的类型参数

c++ - 一个粒子过多:生成了GL_INVALID_VALUE错误。 <start>不满足着色器存储缓冲区的最低对齐要求

c++ - 特征矩阵的就地元素类型转换

c++ - 递归模板模式;类型何时确定?