gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:49:17 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com