我收到以下IntelliSense错误:
表达式必须具有类类型f:\ C ++ \ prj \ map1 \ map1 \ testMap1.cpp 11
这是指我的代码中的以下行(如下所示):
theMap.insert(1, "one");
我无法弄清问题是什么。它似乎与theMap
的声明无关,但每次我尝试在theMap
上调用方法时都会收到错误。这是我的代码:
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;
}
答案 0 :(得分:10)
map<int, string> theMap();
这是声明一个函数,该地图不会调用map
删除()
map<int, string> theMap/*()*/;