Sign in to follow this  

Problems with c++ return list using function

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, This is my first time using list and I have problems using function to return list. This is what I have done... class Player{ public: list<Player> returnList(); ... private: list<Player> lp; }; int main(){ Player ply; list<Player> l; l = ply.returnList(); } list<Player> Player::returnList() { return lp; } The list l will still be empty and I have no idea how to remedy it. I need the list l to have the same values as list lp. Can anybody help?

Share this post


Link to post
Share on other sites
You have several problems:

You are returning By-Value meaning that you are returning a copy of the list. This is very time consuming. You shold probably return a pointer or a reference to the list to save time.

Also I do not see that you put anything in the list before returning the copy. So, why wouldn't it be empty?

In what part of your program do you populate the list?

Share this post


Link to post
Share on other sites
Ok I rewrite the whole thing for better understanding.

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

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

Player::Player(string n, int a){
name=n;
age=a;
}

int main(){
Player ply;
ply.insertList();
list<Player> l;
l = ply.returnList();
}

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

void Player::insertList(){
Player p1("Peter", 25);
Player p2("David", 26);
}

I need the list l to be exactly the same as list lp. Thanks.

Share this post


Link to post
Share on other sites
Quote:


void Player::insertList(){
Player p1("Peter", 25);
Player p2("David", 26);
}



This does nothing to the list. Try:



void Player::insertList(){
Player p1("Peter", 25);
Player p2("David", 26);
lp.push_back(p1);
lp.push_back(p2);
}





But also what SillyCow said...

Share this post


Link to post
Share on other sites
Sorry, I have added push_back.

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

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

Player::Player(string n, int a){
name=n;
age=a;
}

int main(){
Player ply;
ply.insertList();
list<Player> l;
l = ply.returnList();
}

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

list l is still empty.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 code
class 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.



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this