Instancing an object from a vector

Ok I am messing around and trying to get a grip on oop so i created a class called player that is meant to store each players dificulty level and there score but the problem i am having is when i try to create and instance of the object for each player I store the player names in a vector to start then try to use a loop to set up a instance for each player but it doesnt seem to like what i am doing I will post the code below if anyone can tell me what i am doing wrong or even give me some ideahs i would appriciate it ignore the comments its more for me then anything else at some point when im done i might upload the entire source so that people can get an ideah of how to do this not sure though it might be confusing to some one just starting in c++ this is the error that i am getting


------ Build started: Project: BibleTrivia, Configuration: Debug Win32 ------
Compiling...
trivia.cpp
c:\users\admin\documents\visual studio 2008\projects\bibletrivia\bibletrivia\trivia.cpp(52) : error C2057: expected constant expression
c:\users\admin\documents\visual studio 2008\projects\bibletrivia\bibletrivia\trivia.cpp(52) : error C2466: cannot allocate an array of constant size 0
c:\users\admin\documents\visual studio 2008\projects\bibletrivia\bibletrivia\trivia.cpp(52) : error C2133: 'players' : unknown size
Build log was saved at "file://c:\Users\Admin\Documents\Visual Studio 2008\Projects\BibleTrivia\BibleTrivia\Debug\BuildLog.htm"
BibleTrivia - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



here is the class and the function that is supposed to be creating instances

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class player{
	int dificulty;
	int score;
};
void createPlayers()
{
int number;
number = numberOfplayers();
vector<string> players (number);
        for (int i = 0; i < number; i++) 
        { 
            cout << "Player #" << i+1 << " Please enter your name\n"; 
            cin >> players[i];
			system ("CLS");
        }
		        for (int i = 0; i < number; i++) 
        { 
            player players[i]; // this is to create instances of or player object
        }
		

}

and here is the full source
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
/* This is a practice program a built while trying to refresh my memory on some basic c++ 
programing You can use it as you will but do not try to use this source code for your classes 
if you want to download it and study it to get a better ideah of how it works and get some ideas 
for your class work that is fine but the only way to learn C++ is by creating your own ideahs and 
figuring out how to make it work*/

//Start Includes Section of this program 
#include <iostream> // input output library 
// iostream if you dont know what this does go visit 
//http://www.cplusplus.com/reference/iostream/
#include <vector> // vector library 
// This is a good page to explain what vectors are and how you can use them 
//http://www.cplusplus.com/reference/stl/vector/
#include <string>//String Libraray
// to put it simpley if you plan to use the data type string you need to include this 
using namespace std;
//End of include statments

//Start Golbal Variables

//End Global Variables
class player{
	int dificulty;
	int score;
};
int numberOfplayers()//as you can see i expect this funtion to return a number or int to my program
{
	int players; // this is a funtion specific variable and can not be used by any other function because it is inclosed within
	//this function 
	cout << "How many Players will be playing today\n";// This defines a constant string that will be printed to the screen
	//each time the game is played in this case to get the number of players that will be playing
	cin >> players;// This is where we will get the users input and assighn it to the variable players
	return players; // This tells the program that when ever another function calls this funtion that it needs to return the value
	//of  whatever is stored in players in this case a number or integer
}

//Start main function remember that the computer will always run the function called main first 
//and if you do not have a mian funtion your program will not do anything
void createPlayers()
{
int number;
number = numberOfplayers();
vector<string> players (number);
        for (int i = 0; i < number; i++) 
        { 
            cout << "Player #" << i+1 << " Please enter your name\n"; 
            cin >> players[i];
			system ("CLS");
        }
		        for (int i = 0; i < number; i++) 
        { 
            player players[i]; // this is to create instances of or player object
        }
		

}
void main()// The first part of defining a function is to give it a return type because i do  
//not need it to return any thing i used the void type you can also use int char etc depending on 
//your needs i will show you how this works later in this program
{
//everything you want your mai function to do needs to go between these brackets
createPlayers();


}
Last edited on
player players[i];
This does not do what you think it does. This tries to create an array of i player elements. When creating an array its size must be constant. i starts counting from 0 and you cannot create an array of 0 elements.
// this is to create instances of or player object
You have already created number instances on line 43. You don't need this part at all.
Ok i know i have there names but how do i assign them to a class ie they enter 1 player with the name john then it would be

player john;

but how do i get all of the names from the vector and creat seperate instances for the class i created? This way i can use john.addpoint(); etc <--- i have not created that function in my class yet if i change my vector name to player to match my class will that create the instance for each player?
Last edited on
You have a few problems here that I can see.

Firstly, your player class is a bit useless at the moment, because the two fields have private visibility. Without a constructor to set them, and a means to access them, you will never be able to set/get these values.

Secondly, shouldn't the player class also have a member for 'name' (which I guess could be a string)?

Now, as the compiler points out, you have another problem on line 52. You have already declared 'players' to be a vector that stores strings. As 'players' stores string objects, it cannot store player objects. One possible solution is to change the type that the vector is storing to player, i.e.:

vector<player> players (number);

Then, each element of the vector stores a player object. As you go through the loop asking for names, you can take the name entered by a user, use it to dynamically create a new player instance (through a constructor that you can define), and add it to the 'players' vector.

I'm not sure if that will make sense to you, but give it a go. If you get stuck, post your attempt and we'll go from there :).

Actually that makes perfect since why i didnt thing of this before i have no ideah Im going to give it a shot and will get back to you if it blows up in my face lol
Topic archived. No new replies allowed.