I posted this in the beginner forum but with no luck.
Im trying to make a linked list / lobby, but for some reason when I try adding a New element it crashes what am I doing wrong ?
You should not be able to initialize those values within the class. You should get a compiler error saying something like "Only static integral data types can be initialized within a class."
You should initialize those values in the constructor.
Line 48, main should be of type int. Always.
Line 32:
1 2 3
if(phead = 0){ //You use the assignment operator, not the equality operator.
phead = newplayer;
}
You should not be able to initialize those values within the class. You should get a compiler error saying something like "Only static integral data types can be initialized within a class."
If you have a single linked list with one member that points to the next node then it is better and simpler to insert a new element at the beginning of the list. for example
1 2 3 4 5
void addplayer(string name){
player * newplayer = new player(name);
newplayer->setnext( phead );
phead = newplayer;
}
Also it woukd be better to define constructor player the following way
1 2 3
player(string name = "", player *pnext = 0 ) : name( name ), pnext( pnext )
{
}
In this case function addplayer shown above can be rewritten the following way
1 2 3 4
void addplayer(string name){
player * newplayer = new player(name, phead);
phead = newplayer;
}
If you want to add a new element at the end of the single linked list then it would be better to have one more pointer