Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类模板需要模板参数列表_C++_Templates - Fatal编程技术网

C++ 类模板需要模板参数列表

C++ 类模板需要模板参数列表,c++,templates,C++,Templates,我得到了错误-keyedcollection.h(34):错误C2955:“keyedcollection”:使用类模板需要模板参数列表 我在谷歌和其他网站上搜索了几个小时,仍然找不到任何解决这个问题的方法。关于我能做什么有什么建议吗 声明: friend ostream& operator<<(ostream&, const KeyedCollection&); friend ostream&operator操作符应该在类中 template <

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

我在谷歌和其他网站上搜索了几个小时,仍然找不到任何解决这个问题的方法。关于我能做什么有什么建议吗

声明:

  friend ostream& operator<<(ostream&, const KeyedCollection&);

friend ostream&operator操作符应该在类中

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);
}
模板
类键控集合{
公众:
//创建一个空集合
KeyedCollection();
//返回集合中的对象数
int size()常量;
void get_vectorone();
//使用“忽略重复项”策略将具有K类型键的T类型对象插入到集合中
无效插入(常数K和,常数T和);
//输出集合中对象的数据值,每行一个数据值

friend ostream&operatorhint:模板参数列表就是您用来指定模板参数的东西。这就是我的想法,我在很多地方都尝试过。但我不确定该放在哪里。我在参数中思考,但不确定放在哪里。使用了什么K和T?我尝试了Tlambda告诉我的,但现在得到了链接器错误:错误LNK2019。有关于如何修复的线索吗?@user3236062我修复了链接器错误。你想让我如何给你代码?好的,我试过了,但现在我得到了一个链接器错误:LNK2019有关于如何修复的线索吗?你能发布整个源代码吗?如果我看不到,这很难帮助。@user3236062模板函数的定义必须必须在标题中,而不是在cpp文件中。
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);
}