Problems with c++ return list using function

Started by
18 comments, last by SillyCow 15 years, 5 months ago
How do you conclude that l is empty?

I see that "l = ply.returnList();" is your last line of code. Remember that if you set a breakpoint on this line it will be triggered before the command is carried out.

Could this be the problem?

I compiled the code you have posted with Visual C++.
It works fine. l has both players in it


My Oculus Rift Game: RaiderV

My Android VR games: Time-Rider& Dozer Driver

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees

Advertisement
I have made a mistake. It is suppose to be class inheritance. This is the final one. I'm so sorry.

#include <iostream>
#include <list>
#include <fstream>
#include <algorithm>
using namespace std;

class Player{
public:
Player(string="", int=0);
list<Player> returnList();
void insertList();

private:
list<Player> lp;
string name;
int age;
};

class Team : public Player{
public:void readGames();
};
Player::Player(string n, int a){
name=n;
age=a;
}

int main(){
Team myTeam;
myTeam.readGames();
}

list<Player> Player::returnList() {
return lp;
}

void Player::insertList(){
Player p1("Peter", 25);
Player p2("David", 26);
lp.push_back(p1);
lp.push_back(p2);
}
void Team::readGames(){
Player ply;
list<Player> l;
l=ply.returnList();
cout<<l.size()<<endl;
system("pause");
}

It return 0.
You're still not inserting anything to the list. You create a new Player instance, and then immediately call returnList without inserting anything to the list.
Also, it is worth noting that a Team is not a Player. Generally, a Team has Players. Look up object composition as compared with class inheritance.

What you have is Team inheriting from Player and Player composed of other Players. Moving the std::list<Player> to Team would be a start.
I have insert the list as mentioned.

#include <iostream>
#include <list>
#include <fstream>
#include <algorithm>
using namespace std;

class Player{
public:
Player(string="", int=0);
list<Player> returnList();
void insertList();

private:
list<Player> lp;
string name;
int age;
};

class Team : public Player{
public:void readGames();
};
Player::Player(string n, int a){
name=n;
age=a;
}

int main(){
Player myPlayer;
myPlayer.insertList();
Team myTeam;
myTeam.readGames();
}

list<Player> Player::returnList() {
return lp;
}

void Player::insertList(){
Player p1("Peter", 25);
Player p2("David", 26);
lp.push_back(p1);
lp.push_back(p2);
cout<<lp.size()<<endl;
}
void Team::readGames(){
Player pp;
list<Player> l;
l=pp.returnList();
cout<<l.size()<<endl;
system("pause");
}

List lp have size 2 but list l is still 0.
The list returned by pp.returnList() is a different list entirely to the one you are expecting.

Each instance of Player has its own list. Modifications to one will not affect the list contents in other instances.
Of course the size of the list l is zero; you get it from a player for which you have not inserted anything into.
Thanks for reading my codes. But how can I copy the whole list to another class?
Quote:Original post by redbull86
Thanks for reading my codes. But how can I copy the whole list to another class?


look up std::copy.

std::list<int> list1;list.push_back(1);list.push_back(2);list.push_back(3);list.push_back(4);std::list<int> list2(list.size()); // important!! list 2 must be big enough!std::copy(list1.begin(), list1.end(), list2.begin());


or you could create a list from the existing list
// based on your codeclass Player{    const list<Player>& returnList()    {        /// return whatever    }};int main(){   Player p;   list<Plater> listOfPlayers(p.returnList());}


not sure if this does a deep copy or not.



if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
I all depends on what you want to copy. As you know in C++ you have a difference between references(pointers) and values.

1) If the list member type (list <member_type>) is a value (as opposed to a pointer) then copying the list will also copy the values:
list<int> list1;
list <int>list2;
l2=l1;//copy the list and it's members

2) If the list member type is a pointer then copying the list will copy references to the lists member type; meaning if you change the value of an object in the 5th position of list1 it will also change in list2. BUT: if you insert a new object into list1 it will NOT be inserted into list2:

list<int *> list1;
list <int *> list2;
l2=l1;//copy the list and it's references to members

3) If the list itself is a pointer than changing anything in list1 will also change things in list2. This is because list2 will not be a copy of the list but rather a reference to list1. If your game requires speed and you only want to read the contets of list1 using list2 I recommend using this method. This is because making copies of big lists is very CPU consuming, and there is no reason to do it if you are just going to read the list:

list<int > list1;
list <int > *list2;
l2=&l1//l2 is now a refernce to l1;

If you want a true copy (snapshot) of the list that will not change regardless of what you decide to do to list1 later use 1. For instance if you want to create Team2 which is a different team from Team1 but has members which are copies of the players in team1. Like creating "Chicago Bulls2" which is an exact copy of the original Chicago Bulls (with Michael Jordan of-course)

Note: The "by reference / by value" aproach is uniquely important in C and C++. If you are going to use one of these languages it is important to understand these concepts.

My Oculus Rift Game: RaiderV

My Android VR games: Time-Rider& Dozer Driver

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees

This topic is closed to new replies.

Advertisement