linked list

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 ?

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->getnext() != 0){
					iter = iter->getnext();
				}
				iter -> setnext(newplayer);
			}
		}
	private:
		player * phead = NULL;
};


main(){
	lobby lob1;
	lob1.addplayer("Rasmus");
	lob1.addplayer("Søren");
	lob1.addplayer("Malle");
}
First of all, line 23 and 44
 
player * pnext = 0; 

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;
}


Should be if(phead == 0).
Last edited on
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."


AFAIK that is legal as of C++11.
Disch wrote:
AFAIK that is legal as of C++11.
I stand corrected. You learn something new every day. :P

EDIT: For anyone that's interrested:
C++ standard §12.6.2/8:
if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized
as specified in 8.5

Where 8.5 talks about regular initializers.

An example is also provided:
const B b; // error: B has no default constructor
int i; // OK: i has indeterminate value
int j = 5; // OK: j has the value 5
};
Last edited on
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

1
2
3
	private:
		player * phead = 9;
		player * ptail = 0;


and use it to add a new element.
Last edited on
thanks that solved the problem :D
Topic archived. No new replies allowed.