Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


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.

  • You cannot reply to this topic
8 replies to this topic

#1 Deek880   Members   -  Reputation: 124

Like
0Likes
Like

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



Sponsor:

#2 Godmil   Members   -  Reputation: 744

Like
0Likes
Like

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.


#3 Buckeye   Crossbones+   -  Reputation: 5680

Like
0Likes
Like

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.


#4 L. Spiro   Crossbones+   -  Reputation: 14009

Like
3Likes
Like

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.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 ChaosEngine   Crossbones+   -  Reputation: 2449

Like
0Likes
Like

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

#6 Deek880   Members   -  Reputation: 124

Like
0Likes
Like

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.



#7 SunDog   Members   -  Reputation: 232

Like
0Likes
Like

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



#8 Deek880   Members   -  Reputation: 124

Like
0Likes
Like

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.



#9 ChaosEngine   Crossbones+   -  Reputation: 2449

Like
2Likes
Like

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.



PARTNERS