getting error expression must have class type in c++ -
i getting following intellisense error:
expression must have class type f:\c++\prj\map1\map1\testmap1.cpp 11
which referring following line in code (shown in full below):
themap.insert(1, "one");
i cannot figure out issue is. not seem related declaration of themap
, every time try call method on themap
error. here code:
map1.h
#ifndef map_h #define map_h #include <list> #include <utility> using namespace std; //pair class definition template<typename f, typename s> class pair { public: pair(const f& a, const s& b); f get_first() const; s get_second() const; private: f first; s second; }; template<typename f, typename s> inline pair<f, s>::pair(const f& a, const s& b):first(a),second(b){} template<typename f, typename s> inline f pair<f, s>::get_first() const { return first; } template<typename f, typename s> inline s pair<f, s>::get_second() const { return second; } template<typename k, typename v> class map { public: map(); void insert(const k& key, const v& value); bool contain_key(const k& key); v value_of(const k& key); void remove_key(const k& key); void print(); private: list<pair<k, v>> thelist; }; template<typename k, typename v> inline map<k, v>::map():{} template<typename k, typename v> inline void map<k, v>::insert(const k& key, const v& value) { bool notthere = true; if(contain_key(key)) { notthere = false; } if(notthere) { thelist.push_back<pair<key, value>> } } template<typename k, typename v> inline bool map<k, v>::contain_key(const k& key) { iterator iter = thelist.begin(); k temp; for(int x=0 : x< thelist.size() ; x++) { temp = iter->first; if(temp == key) { return true; } iter.advance(); } return false; } template<typename k, typename v> inline v map<k, v>::value_of(const k& key) { iterator iter = thelist.begin(); k temp; for(int x=0; x < thelist.size() ; x++) { temp = iter->first; if(temp == key) { return iter->second; } } cout << “we don’t have key " << key << " in map” "\n"; return 0; } template<typename k, typename v> inline void map<k, v>::remove_key(const k& key) { iterator iter = thelist.begin(); k temp; for(int x=0; x < thelist.size() ; x++) { temp = iter->first; if(temp == key) { thelist.erase(iter) } } } template<typename k, typename v> inline void map<k, v>::print() { iterator iter = thelist.begin; k temp; v temp2; for(int x=0; x < thelist.size() ; x++) { temp = iter->first; temp2 = iter->second; cout << "key:" << temp << " value:" << temp2 << "\n"; } } #endif;
testmap1.cpp
#include "map1.h" #include <string> #include <iostream> using namespace std; int main() { map<int, string> themap(); themap.insert(1, "one"); themap.insert(2, "two"); themap.insert(2, "double"); themap.insert(3, "three"); themap.print(); themap.remove_key(3); cout << "please enter number" << "\n"; int temp; cin >> temp; bool contains = themap.contain_key(temp); if(contains) { cout << themap.value_of(temp); } else { cout << "we don’t have key " << temp << " in map" << "\n"; } return 0; }
map<int, string> themap();
this declaring function themap not invoke default constructor of map
remove ()
map<int, string> themap/*()*/;
Comments
Post a Comment