gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:53 25 4
gpt4 key购买 nike

我收到以下 IntelliSense 错误:

Expression must have class type f:\C++\prj\map1\map1\testMap1.cpp 11

这是指我的代码中的以下行(完整显示如下):

theMap.insert(1, "one");

我无法弄清楚问题是什么。它似乎与 theMap 的声明无关,但每次我尝试调用 theMap 上的方法时,我都会收到错误消息。这是我的代码:

map 1.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();

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

删除 ()

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

关于c++ - 获取错误表达式必须具有 C++ 中的类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com