Jump to content
  • Advertisement
Sign in to follow this  
redbull86

Problems with c++ return list using function

This topic is 3621 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
Advertisement
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
Oh I have put something to the list. Let's say I put

Player p1("Peter", 25);
Player p2("David", 26);

in main().
How do I copy the entire list to another function?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!