Problems with c++ return list using function
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?
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?
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?
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?
Player p1("Peter", 25);
Player p2("David", 26);
in main().
How do I copy the entire list to another 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.
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...
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?
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.
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
Popular Topics
Advertisement