Hi,

I have two classes, Yatzy and YatzyPlayer. In YatzyPlayer.h i create vector<int> mResults and in the constructor in (YatzyPlayer.cpp) i resize it by mResults.resize(15,-2). I also have a function to return this vector declared like vector<int> getResults() and returns mResults. Then in my Yatzy file I'd like to have a vector with this class to keep track on the different players. So in Yatzy.h i declare vector<YatzyPlayer> mNbrPlayers and in the constructor in Yatzy.cpp i add a player: mNbrPlayers.push_back(YatzyPlayer()). The thing is now when i try to call the method getResults() it returns a vector of zero size, why is that? When I print out cout << YatzyPlayer().getResults().size(); it returns 15 which I want to, so anyone have a guess at what I'm doing wrong?

Below is the code I'm working with.

I've deleted some irrelevant code to to make it smaller. If you'd like to see all I'll post it.

#ifndef YATZYPLAYER_H_
#define YATZYPLAYER_H_

#include <vector>
#include <iostream>

class YatzyPlayer
{
	public:

		YatzyPlayer(); //Konstruktor
		YatzyPlayer(const YatzyPlayer& o); //Kopierings konstruktor
		~YatzyPlayer(); //Destruktor

		YatzyPlayer& operator=(YatzyPlayer& o); //Tilldelningsoperatorn

		std::vector<int> getResults(); //Returnerar resultat vektorn

	protected:
	private:
		
		std::vector<int> mResults;
		
};
#include "YatzyPlayer.h"
#include "YatzyRules.h"

YatzyPlayer::YatzyPlayer() //Konstruktor
{
	mResults.resize(15,-2);
}

YatzyPlayer::YatzyPlayer(const YatzyPlayer& o) //Kopieringskonstruktor
{}

YatzyPlayer::~YatzyPlayer() //Destruktor
{}

YatzyPlayer&
YatzyPlayer::operator=(YatzyPlayer& o) //Tilldelningsoperator
{
	return *this;
}

vector<int> YatzyPlayer::getResults()
{
	return mResults;
}
#ifndef YATZY_H_
#define YATZY_H_

/**
* Denna klass skter spelets frlopp
* @author Christian Andersson
*/

#include "Draw.h"
#include "YatzyPlayer.h"
#include <vector>
#include <string>

class Yatzy {

	public:

		Yatzy(); //Konstruktor
		Yatzy(const Yatzy& o); //Kopierings konstruktor
		~Yatzy(); //Destruktor

		Yatzy& operator=(Yatzy& o); //Tilldelningsoperatorn
		void startGame(); //Startar en runda

	protected:
	private:

		std::vector<YatzyPlayer> mNbrPlayers;
};

#endif
#include "Yatzy.h"
#include "YatzyRules.h"

Yatzy::Yatzy() //Konstruktor
{
	mNbrPlayers.clear();
	mNbrPlayers.push_back(YatzyPlayer());
	
}

Yatzy::Yatzy(const Yatzy& o) //Kopieringskonstruktor
{}

Yatzy::~Yatzy() //Destruktor
{}

Yatzy&
Yatzy::operator=(Yatzy& o) //Tilldelningsoperator
{
	return *this;
}


void Yatzy::startGame()
{
	cout << mNbrPlayers.at(0).getResults().size() << endl; //Returns 0
	cout << YatzyPlayer().getResults().size() << endl; //Returns 15
}

In this last code snippet in startGame the above statement returns zero and the other YaztyPlayer().getResults().size() returns 15. I'd like the first one to return 15 aswell. Thanks

/Christian

Ancient Dragon commented: Thanks for using code tags correctly on first post :) +36

>>vector<int> YatzyPlayer::getResults()

That function should return a reference to the vector, not a copy of it. vector<int>& YatzyPlayer::getResults() See the reference operator &

Ok, I'll have a look at that. But how come that line 27 in the last code snippet returns the correct (in my eyes and that I want it to) but not that on line 26? Shouldn't it return the same?

Your code doesn't compile. The compiler hates the line I commented out below.

Yatzy::Yatzy() //Konstruktor
{
	mNbrPlayers.clear();
//	mNbrPlayers.push_back(YatzyPlayer());
	
}

The problem is in operator= method. You need to make the parameter const YatzyPlayer& operator=(const YatzyPlayer& o);

Thank you for your help. It works now, I also changed my copy constructor (commented it out :) ). So it works, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.