homey
homey

Reputation: 41

vector of classes in C++

I am trying to fill vector of classes using the class constructor but I am getting compilation error:

1>c:\users\admin\documents\visual studio 2010\projects\file_io_and_main_argv\file_io_and_main_argv\file_io_argv.cpp(121): error C2663: 'std::vector<_Ty>::push_back' : 2 overloads have no legal conversion for 'this' pointer

The code I am using in the main program of C++ for Class Player is:

const vector<Player> players;  // vector list of players

  players.push_back(Player(ID,pname,WINS,LOSSES,POSX,POSY));  //insert player into vector of players

The error is on the point "." between players and push_back.

Upvotes: 1

Views: 746

Answers (2)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

Your players vector is declared as a const:

const vector<Player> players;

const means exactly that: constant. You can't change it. Can't push_back(), can't erase() existing elements in the vector, can't do anything to change the contents of the vector.

Remove the const keyword from the declaration.

Upvotes: 3

Starl1ght
Starl1ght

Reputation: 4493

You cannot modify const vector. Remove const in const vector<Player> players;

Upvotes: 2

Related Questions