• Create Account

## initializing a vector c++

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Deek880  Members

Posted 09 March 2014 - 08:09 PM

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

### #2Godmil  Members

Posted 09 March 2014 - 08:56 PM

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.

Edited by Godmil, 09 March 2014 - 08:59 PM.

### #3Buckeye  GDNet+

Posted 09 March 2014 - 08:58 PM

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.

Edited by Buckeye, 09 March 2014 - 09:26 PM.

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.

### #4L. Spiro  Members

Posted 09 March 2014 - 09:28 PM

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

Edited by L. Spiro, 09 March 2014 - 09:29 PM.

### #5ChaosEngine  Members

Posted 09 March 2014 - 09:42 PM

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

### #6Deek880  Members

Posted 10 March 2014 - 11:05 AM

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.

### #7SunDog  Members

Posted 10 March 2014 - 11:52 PM

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

### #8Deek880  Members

Posted 11 March 2014 - 07:19 PM

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.

### #9ChaosEngine  Members

Posted 11 March 2014 - 08:17 PM

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.

Edited by ChaosEngine, 12 March 2014 - 06:44 PM.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.