获取错误表达式必须具有c++中的类类型
getting error expression must have class type in c++
我得到以下智能感知错误:
表达式必须有类类型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/*()*/;
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型