基本操作数具有非指针类型 G++ 编译器错误
base operand has non pointer type g++ compiler error
在下面的代码中,我收到错误:
city.cc: In member function ‘std::vector<std::basic_string<char> > MyCity::get_neighbours()’:
city.cc:25:42: error: base operand of ‘->’ has non-pointer type ‘std::pair<MyCity*, double>’
In file included from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:65:0,
from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/char_traits.h:41,
from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ios:41,
from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from city.cc:1:
/depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = std::basic_string<char>; _U2 = double; _T1 = MyCity*; _T2 = double]’:
city.cc:18:50: required from here
/depotbld/RHEL5.5/gcc-4.7.2/bin/../lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_pair.h:111:39: error: cannot convert ‘const std::basic_string<char>’ to ‘MyCity*’ in initialization
.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class MyCity {
string name;
std::vector<pair<MyCity*,double> > neighbours;
public:
MyCity()
{
// neighbours.clear();
}
MyCity(string s, string s1, double d)
{
name = s;
neighbours.push_back(std::make_pair(s1,d));
}
std::vector<string> get_neighbours( )
{
std::vector<string> names;
for (size_t i = 0; i< neighbours.size(); ++i)
{
names.push_back(neighbours[i]->first->get_name());
}
return names;
}
};
class MyState {
vector<MyCity*> cities;
string name;
public:
MyState() { }
MyState(string s) {
name =s;
}
bool add_city(string name, string neigh, double d)
{
MyCity* c = new MyCity(name,neigh,d);
cities.push_back(c);
return true;
}
};
不要取消引用std::pair
,因为它不是指针。
std::vector<string> get_neighbours( )
{
std::vector<string> names;
for (size_t i = 0; i< neighbours.size(); ++i)
names.push_back(neighbours[i].first->get_name());
return names;
}
你的构造函数也有一个问题,std::make_pair(s1, d)
将返回一个std::pair<std::string, double>
,所以它不能被推回你的邻居向量。
尝试这样的事情:
MyCtiy(string s)
:name(s)
{
}
MyCity(string s, string s1, double d)
:name(s)
{
created_neighbours.emplace_back(new neighbour(s1));
MyCity* city = created_neighbours.back().get();
neighbours.push_back(std::make_pair(city, d));
city->addNeighbour(this, d);
}
private:
std::vector<std::unique_ptr<MyCity>> created_neighbours;
void addNeighbour(MyCity* city, double d)
{
neighbours.push_back(std::make_pair(city, d));
}
注: 如果不希望关联是多对多的,则可以剥离 addNeighbord 部分。
编辑:修复addNeighbor以提供MyCity指针。创建了一个created_neighbours集合来存储(和免费)创建的邻居。
可以在
代码中找到3个错误。请在代码中找到并更正以下 2 行
neighbours.push_back(std::make_pair(some_pointer_to_the_MyCity_object,d));
names.push_back(neighbours[i].first->get_name());
并且您必须在MyCity类中实现get_name()函数
现在应该编译了
相关文章:
- 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作为变体中的替代类型之一
- 类中的字符串不命名类型