下面的代码给了我错误的 g++ 分配:
std::bad_alloc what(): "St9bad_alloc"
。我无法理解原因,因为这是我要添加的第一个元素,vector
因此不应该存在内存限制问题
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class MyCity {
string name;
std::vector<pair<string,double> > neighbours;
public:
MyCity(string s)
{
name =s;
// neighbours.clear();
}
MyCity(string s, string s1, double d)
{
name = s;
neighbours.push_back(std::make_pair(s1,d));
}
};
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);
}
bool add_city(string name)
{
MyCity* c = new MyCity(name);
cities.push_back(c);
return true;
}
};
int main()
{
MyState* ss;
ss->add_city("agra");
return 0;
}
谢谢鲁奇