我正在尝试在cpp中构建一个使用2个模板argumemts()的字典,并且我有一个“获取”功能,该功能应仅返回该值。
这是类标题:
#include <iostream>
#include <vector>
using namespace std;
template <class K, class V>
class Dict {
protected:
vector<K> keys;
vector<V> values;
K Key;
V Value;
public:
Dict();
void set(K Key, V Value);
V get(string key);
};
这是cpp类:
#include "Dict.h"
template <class K, class V>
Dict<K,V>::Dict() {};
template <typename K, typename V>
V Dict<K,V>::get(K key) {
V lol;
bool found = false;
for(int i = 0; i < this->keys.size(); i++){
if(this->keys[i] == key){
return *this->values[i];
}
}
cout << "This Key does not exists";
return lol;
}
template <typename K, typename V>
void Dict<K,V>::set(K Key, V Value) {
keys.push_back(Key);
values.push_back(Value);
}
template class Dict<string, int>;
这是主要的:
#include "Dict.h"
int main() {
Dict<string, int> d;
d.set("yoav", 34);
d.set("hey", 8);
int num = d.get("hey");
cout << num;
return 0;
}
编译器将抛出此错误:
error: prototype for ‘V Dict<K, V>::get(K)’ does not match any in class ‘Dict<K, V>’
V Dict<K,V>::get(K key) {
^
答案 0 :(得分:0)
除此之外,您还应该将模板实现不放在cpp文件中,而应该放在标头中,这是另一个错误。
if (${no} == 1)
store | ${!col1} | test
endif
if (${no} == 2)
store | ${!col2} | test
endif
if (${no} == 3)
store | ${!col3} | test
endif
应该是
return *this->values[i];
或
return this->values[i];
以及有效的(=编译)代码
return (*this).values[i];