initializing a vector c++

Started by
7 comments, last by ChaosEngine 10 years, 1 month ago

I have a game class in which I want to initialize a vector of players: Please note that this has to be a reference: It goes like this:


#define GAME_H
#include "player.h"
#include <vector>
#include <iostream>
#include <iterator>

class game
{

public:

	game();
	~game();

	player P;
	std::vector<player>&gPlayerVec;
	std::vector<player>gPlayerVecCopy;

	void PlayGame();




};
#endif //GAME_H

I get the following error when I compile:

c:\mpgames2\pointer\game.cpp(9) : error C2758: 'gPlayerVec' : must be initialized in constructor base/member initializer list
c:\mpgames2\pointer\game.h(17) : see declaration of 'gPlayerVec'
c:\mpgames2\pointer\game.cpp(9) : error C2758: 'gPlayerVecCopy' : must be initialized in constructor base/member initializer list
c:\mpgames2\pointer\game.h(18) : see declaration of 'gPlayerVecCopy'

the game constructor is here:


game::game()
{
	

	gPlayerVec.push_back(P);

}

What do I need to put in there to fix this problem

Advertisement

I may be wrong, but I don't think you can initialise references like that. Is there a vector that it is actually referencing? If so how about putting the line you have for initialising the reference into the argument of the constructor? So you can pass in the reference as you create the game class.

Although it seems to appear as a variable, you have to think of a reference as an alias for an existing value. (Although a function declaration can have a reference as an argument, the actual reference is constructed from an existing value when the function is actually called.) Just as "John Doe" can be an alias only if there's a person with another name, a reference must be defined when it's declared.

EDIT: Perhaps if you explain what problem you're dealing with, an alternate solution can be suggested.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Please note that this has to be a reference

I think you mean that the vector must contain references of type “player”.

To fix the compiler errors:


game::game( std::vector<player> &_vPlayerVec, std::vector<player> &_vPlayerVecCopy ) :
    gPlayerVec( _vPlayerVec ),
    gPlayerVecCopy( _vPlayerVecCopy ),
{

}
But this isn’t what you actually want to do.


You want to:

#define GAME_H
#include "player.h"
#include <vector>
#include <iostream>
#include <iterator>

class game
{

public:

	game();
	~game();

	player P;
	std::vector<player *>gPlayerVec;
	std::vector<player *>gPlayerVecCopy;

	void PlayGame();

};
#endif //GAME_H
game::game()
{
	

	gPlayerVec.push_back(&P);

}
Put pointers to players into the vector.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


Please note that this has to be a reference:

Ok, first question: why? Is it using an object that someone else owns? In which case it absolutely should be a reference (maybe even a const reference, if you're just iterating over the collection). If that is the case, then the compiler is giving you the answer. Your reference must be initialised when the containing object is constructed. The simplest way to do this is to pass vector you want to refer to in the constructor arguments as L.Spiro has demonstrated.

If, on the other hand you actually want polymorphic behaviour, you should follow L. Spiros second example, but just be aware of who has the responsibility for the memory (i.e. the ownership semantics).

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

I think Spiro answered the question. I haven't tried to implement his code yet, (but thank you for the help spiro) but I think he is exactly right

What I'm trying to do this: I have a fixed size array of players in player class, then I have a vector of player waiting to get into the game, gPlayerVec, then the game players are in gPlayerVecCopy that are active, This is a vector with players back inserted from gPlayerVec.

When a player sits out he is poped out of the vectors, but the player array keeps track of what would have been his opportunities. When the player comes back in he is pushed back into the vector.

The vectors aren't recognizing the changes that have been made to the player when he is put back in. Or lets say when I try to change a variable directly from the underlying player in the player array, the vectors of players aren't picking up on it.

I think Spiro answered the question. I haven't tried to implement his code yet, (but thank you for the help spiro) but I think he is exactly right

What I'm trying to do this: I have a fixed size array of players in player class, then I have a vector of player waiting to get into the game, gPlayerVec, then the game players are in gPlayerVecCopy that are active, This is a vector with players back inserted from gPlayerVec.

When a player sits out he is poped out of the vectors, but the player array keeps track of what would have been his opportunities. When the player comes back in he is pushed back into the vector.

The vectors aren't recognizing the changes that have been made to the player when he is put back in. Or lets say when I try to change a variable directly from the underlying player in the player array, the vectors of players aren't picking up on it.

Because you are using a vector of players when you should be using a vector of pointers or references to players.

E.g.


vector<Player> player_vec;
Player p("someplayer")
p.setFoo(0);
 
player_vec.push_back(p);
p.setFoo(1);
 
Player p2 = player_vec[0];   
p2.getFoo();  /// returns 0
 

This shoudl be somethign like


vector<Player*> player_vec;
Player p("someplayer")
p.setFoo(0);
 
player_vec.push_back(&p);
p.setFoo(1);
 
Player *p2 = player_vec[0];   
p2->getFoo();  /// returns 1

Edit: Basically what Spiro suggested

Actually Spiro's method of initializing a reference to a vector didn't compile 10 errors. But I'm using visual c++6.0 which is a 1998 compiler, even though it was current up to 2004.

This works, its a mix of both suggestions:

game.h

std::vector<player *>playervec;

player * P

game.cpp

game::game()

{

P = new Player;

gPlayerVec.push_back(P);

}

Now, a change works across all vectors and the underlying player whether initiated from the player or the vector.

Question, Is it worth re-writing the whole program which has grown up with a constant array of Players?

I can accomplish the same thing with an I & J loop setting the requisite elements equal to each other at the end of every game run loop.


I'm using visual c++6.0

Stop right there. There have been multiple free versions of Visual C++ since then, all of which are better than 6.0. Here's the latest one.

Do your self a favour and get a newer version before you write another line of code. I'm not even slightly kidding.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement