-3

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);
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • 2
    Ask yourself, what `vector` object does `strategies` point to? – NathanOliver May 25 '21 at 17:57
  • 1
    `strategies->push_back(name);` is nonsense without a real vector behind that `strategies` pointer. There is none in this code. `strategies` is an indeterminate pointer. dereferencing it, including for member function execution, invokes *undefined behavior*. – WhozCraig May 25 '21 at 17:57
  • after all i just realized I forgot to add new vector(),I'm kinda new to C++,thanx anyway for your help – Seif Eddine Segueni May 25 '21 at 18:08

1 Answers1

1

I hope you'd have declared string name;

second, you are creating a pointer but not doing new() of some vector to which *strategies pointer really points. 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

dixit_chandra
  • 468
  • 3
  • 14