Problems with return a vector (I think)

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?

/Christian
Can I see what your code is?
Yes, I've deleted some irrelevant code to to make it smaller. If you'd like to see all I'll post it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
		
};

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
#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;
}

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
#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


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
#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
Hm, try this instead for your constructor:

1
2
3
4
5
6
Yatzy::Yatzy() //Konstruktor
{
	mNbrPlayers.clear();
	YatzyPlayer newPlayer;
	mNbrPlayers.push_back(newPlayer);
}
That doesn't work either :(
Because your copy constructor does nothing, and it is being called.

You don't need a copy constructor; just remove it. The default one provided by the compiler is sufficient.
Thank you! It worked. But If I'd like to write my own copy constructor how would I do that? Thanks again
The default copy constructor provided by the compiler does a member-wise copy of all the elements. Yours has to do the same.

1
2
3
4
5
Yatzy::Yatzy( const Yatzy& that ) : 
   mNbrPlayers( that.mNbrPlayers )  // Invokes copy constructor of the only class member
   // You'd do the same for the rest of your members here, separated by commas.
{
}


Things start getting trickier when you have raw pointers as data members and you need to take "deep" copies (ie, copy what the pointer points to rather than the pointer value itself).

EDIT: I just noticed your assignment operator has the same problem: it does nothing. (Also, it does not have the right signature):

1
2
3
4
5
6
7
8
9
10
11
Yatzy& operator=( Yatzy that )  // Or const Yatzy&, but NOT (non-const) Yazty&
{
    std::swap( mNbrPlayers, that.mNbrPlayers );
    // If passing by const reference, then instead of above line, do:
    //    mNbrPlayers = that.mNbrPlayers;
    // However understand that this function would not be exception-safe
    // if more data members are ever added.  As I've written it, as long
    // as you use the copy-swap idiom as I've done, this method will be
    // exception-safe.
    return *this;
}

Last edited on
Thank you again!
Topic archived. No new replies allowed.