在以下代码中,我收到错误:
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;
}
};