[英]terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc after pushing back into a vector
I'm facing a problem here, I've declared a pointer to a vector.我在这里遇到了一个问题,我已经声明了一个指向向量的指针。 After element insertion, it shows me this error
插入元素后,它向我显示此错误
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Here is my code:这是我的代码:
vector<string>* strategies;
cout << "Name of the player 1 : ";
cin >> name;
strategies->push_back(name);
I hope you'd have declared string name;
我希望你已经声明了
string name;
second, you are creating a pointer but not doing new()
of some vector to which *strategies
pointer really points.其次,您正在创建一个指针,但没有执行
*strategies
指针真正指向的某个向量的new()
。 if all your purpose is to insert an element in vector then you really don't need a pointer rather you need object of vector<string>
code如果您的所有目的是在向量中插入一个元素,那么您真的不需要指针,而是需要
vector<string>
的 object代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.