Getting an error expression should have a class type in C ++

I get the following IntelliSense error:

The expression must be of class type f: \ C ++ \ prj \ map1 \ map1 \ testMap1.cpp 11

As for the following line in my code (shown below):

theMap.insert(1, "one");

I can’t understand what the problem is. This doesn't seem to be related to theMap , but every time I try to call a method on theMap , I get an error. Here is my 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 this key " << key << " in the 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 a 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 this key " << temp << " in the map" << "\n"; } return 0; } 
+4
source share
1 answer
 map<int, string> theMap(); 

this declares theMap function not to call the default constructor map

remove ()

 map<int, string> theMap/*()*/; 
+9
source

All Articles