获取错误表达式必须具有c++中的类类型

getting error expression must have class type in c++

本文关键字:c++ 类型 取错误 表达式 获取      更新时间:2023-10-16

我得到以下智能感知错误:

表达式必须有类类型f: c++ prjmap1map1testMap1.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;
}
map<int, string> theMap();

这是声明一个函数,map不调用map的默认构造函数

remove ()

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