[C++] How exactly would one initialise a vector which is being created by a forward declaration?

Trying to create a vector of cSquare however I am having difficulties as the class states it needs to be initalised? How would one go about doing that considering if it wasn't in the class I would not need two?

http://pastebin.com/JCazYzPJ
If a class member is a reference, it must be initialized in the initializer list of the constructors. E.g.
1
2
3
4
5
6
7
8
class A{
    int &i;
public:
    A(int &);
};

A::A(int &param): i(param){
}
Of course, it's not possible to rebind a reference member once the object has been constructed. If you need to do this, you need a pointer.
Hi thank you for the reply however I have done what you have said but I still receive a Visual Studios error saying the follow:

Error 2 error C2664: 'void std::vector<cSquare *,std::allocator<_Ty>>::push_back(cSquare *const &)' : cannot convert argument 1 from 'int' to 'cSquare *&&' c:\users\connor\documents\visual studio 2013\projects\monopolyassignment\cplayer.cpp 11


Error 1 error C2758: 'cPlayer::vOwnedProperties' : a member of reference type must be initialized c:\users\connor\documents\visual studio 2013\projects\monopolyassignment\cplayer.cpp 7

Here is what the player.cpp looks like where the errors are.

1
2
3
4
5
6
7
8
9
10
cPlayer::cPlayer(string inputPlayerName, float inputPlayerBalance, int inputCurrentLocation, vector<cSquare*>& vOwnedProperties) : vOwnedProperties(vOwnedProperties)
{
	setPlayerBalance(inputPlayerBalance);
	setPlayerName(inputPlayerName);
	setPosition(inputCurrentLocation);
}
void cPlayer::setPropertyOwnership(cSquare* cPropertyAddress)
{
	vOwnedProperties.push_back(cPropertyAddress);
}


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
#ifndef CPLAYER_H
#define CPLAYER_H

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class cSquare;

class cPlayer
{
private:
	string playerName;
	float playerBalance;
	int currentPosition;
	vector<cSquare*>& vOwnedProperties;
	cSquare* cPropertyAddress;
protected:
	cSquare* cSquareRecieved;
public:
	cSquare* getcSquare();
	cPlayer(string, float, int, vector<cSquare*>&);
	void setPlayerName(string);
	void setPlayerBalance(float);
	void setPropertyOwnership(cSquare*);
	int getPropertiesAmount();
	cSquare* getProperties();
	void setPosition(int);
	string getPlayerName();
	float getPlayerBalance();
	int getCurrentPosition();
	virtual ~cPlayer();
};
#endif 


Can you see where I have gone wrong? Thank you for the reply also
I don't see anything wrong in the code you've posted.
1
2
3
4
5
6
7
8
9
10
// using namespace std; // avoid in header files

class cPlayer
{

	// vector<cSquare*>& vOwnedProperties;
        std::vector<cSquare*> vOwnedProperties ; // not a reference

        // ...
};
Topic archived. No new replies allowed.