Problems with c++ return list using function

Started by
18 comments, last by SillyCow 15 years, 5 months ago
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?
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?

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

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?
Oh sorry not in main(). Should be in another class function.
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.
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...
You are not inserting anything into lp.
I do not see any lp.insert(...) calls in your code.

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

I have tried using push_back but list l is still empty. It return 0 when I use l.size(); Anybody knows what is the problem?
Please post some example code. So that we can see the problem

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

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.

This topic is closed to new replies.

Advertisement