i am trying to build a dictionary in cpp that takes 2 template argumemts (), and i have a "get" function that should return the value only.
this is the class header:
#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);
};
this is the class 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>;
this is the main:
#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;
}
and the compiler throws this:
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) {
^