Wrong with Vector
Jul 19, 2011 at 7:16pm UTC
Hi guys!
Can anyone tell me what is wrong with the following program.....it supposed to be a program where you can input a chosen amount of players and then show all the players on the screen. I dont want the vector to be part of the class but cant seem to make it work, keep getting the same error:
error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'void' to 'Player &&'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Player {
public :
void setPlayer (string name, int age);
void printPlayer();
private :
string n;
int a;
};
void Player::setPlayer(string name, int age) //Read in player
{
name = n;
age = a;
cout << "Name: " ;
getline(cin, name);
cout << "Age: " ;
cin >> age;
cin.get();
};
void Player::printPlayer() //output player
{
cout << endl << endl;
cout << "Namn : " << n << ", " << "Ã…lder : " << a << endl;
};
int main()
{
Player player;
vector<Player> Pvec; //vector which reads in all players
string n;
int c;
int a = 0;
cout << "1. Add player\n"
<< "2. Show players\n"
<< "0. Quit " << endl;
cin >> c;
cin.get();
do
{
if (c == 1)
{
Pvec.push_back(player.setPlayer(n, a));
cout << "1. Add player\n"
<< "2. Show players\n"
<< "0. Quit " << endl;
cin >> c;
cin.get();
}
else if (c == 2)
{
player.printPlayer();
}
else
return 0;
}while (c != 0);
cin.get();
}
Jul 19, 2011 at 7:21pm UTC
think about what the vector holds and what your setPlayer function returns, then take a look at line 50 of your code.
Topic archived. No new replies allowed.