problem with a linked list
Im trying to make a linked list / a lobby but for some reason when i try to add a new player the program crashes.
what am I doing wrong ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include<iostream>
#include<string>
using namespace std;
class player{
public:
player(string x = ""){
name = x;
}
string getname(){
return name;
}
player * getnext(){
return pnext;
}
void setnext(player * x){
pnext = x;
}
private:
string name;
player * pnext = 0;
};
class lobby{
public:
void addplayer(string name){
player * newplayer = new player(name);
if(phead = 0){
phead = newplayer;
}
else{
player * iter = phead;
while(iter != 0){
iter = iter->getnext();
}
iter -> setnext(newplayer);
}
}
private:
player * phead = NULL;
};
main(){
lobby lob1;
lob1.addplayer("Rasmus");
lob1.addplayer("Søren");
lob1.addplayer("Malle");
}
|
Topic archived. No new replies allowed.