Help with linked list - for google science fair

I have been using a linked list but when a function is supposed to return the character string it always has a runtime error and breaks because it doesn't work, the function is getData

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
#ifndef CNODE_H
#define CNODE_H

class cNode{

	friend class cList;								// allow the list class access to the private data members of listNode
	friend class Brain;
																
public:

	cNode (char *info);
	char *getData();
	cNode getNextNode();


private:

	char *data;												//information included in the listNode
	cNode *nextNode;									//a pointer link to the next element of the list
	char *linkID;												//an ID for the listNode that can be checked when searching

};


cNode::cNode(char *info) : data(info), nextNode(0){

		

}


char *cNode::getData() {

	return data;	//it breaks here      WHY???????????????

}


cNode cNode::getNextNode(){

	return *nextNode;

}

#endif 
It's difficult to tell what the problem is with only that much info.
Could you show us the way you use your linked list?

Also, if you want a list of strings, you can just use std::list<std::string>
and focus on other, more important parts of your project, unless,
of course, implementing a string list is the main part of your project.

Useful links:

http://cplusplus.com/reference/string/string/
http://cplusplus.com/reference/stl/list/
Last edited on
I have compiled your code, and it works fine. Can you paste the wrong message?
By the way, I am using gcc 4.5.0.
It was being used by another class, I figured out the problem, a control structure wasn't written right so it kept trying to access data that didn't exist :\ but I have a bigger problem now, after I solved the problem I presented above, I saved a copy of each of the resource files to my desktop, and my compiler flipped out Im getting 80 errors that don't make any sense


Error 25 error C2065: 'lastNode' : undeclared identifier

Error 35 error C2065: 'currentNode' : undeclared identifier

Error 40 error C2079: 'Brain::knowledgeList' uses undefined class 'kList'

when before I saved the files, everything was fine and I have included all of the right resource files...


please help
I'm afraid that you have to paste your code, the error is obvious. You haven't include the .h files.
I have numerous times, nothing changed at all, I just went through the list of resource files -> save as, then after that compile, and nothing worked, nothing changed, no other code was written nothing
Last edited on
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#ifndef KLIST_H
#define KLIST_H

#include "kNode.h"
#include "Thing.h"

class kList{

	friend class Brain;

public:

	kList();
	~kList();
	void add(char *);						//add a node at the end of the list
	bool remove();										//remove a node from the back of the list
	bool isEmpty();
	int getLength();

private:

	kNode *firstNode;
	kNode *lastNode;
	kNode *getNewNode(char *);

};


kList::kList() : firstNode(0), lastNode(0){
	
}


kList::~kList(){

	if (!isEmpty()){

		std::cout<<"destroying all nodes in the list\n";
		kNode *currentNode = firstNode;
		kNode *tempNode;

		while (currentNode != 0){

			tempNode = currentNode;

			currentNode = currentNode->nextNode;

			delete tempNode;

		}

	}
	std::cout<<"All Nodes were destroyed\n";

}

void kList::add(char *id){

	kNode *newNode = getNewNode(id);

	if (isEmpty()){							//if the list is empty

		firstNode = lastNode = newNode;

	}
	else{									//if its not

		lastNode->nextNode = newNode;
		lastNode = newNode;

	}

}

kNode *kList::getNewNode(char *id){

	return new kNode(id);

}

bool kList::isEmpty(){

	return firstNode == 0;													//if the first pointer is = 0 (it doesnt point to anything

}

int kList::getLength(){

	kNode *currentNode = firstNode;

	if (!isEmpty()){

		for (int counter = 1; currentNode->nextNode != 0; counter++){
		
			if (currentNode = lastNode){

				return counter;

			}

		}
	}

	else{

		return 0;

	}

}

#endif


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef KNODE_H
#define KNODE_H

#include "Thing.h"
#include "kList.h"
#include <string>
using namespace std;

class kNode{

	friend class kList;
	friend class Brain;

public:

	kNode(char *);
	char *getPropertiesListNode();				//returns the first pointer from the proporties list
	char *getId();								//returns the ID of the Node (contains the thing class)
	void addProperty(char *);
	void setId(char *);
	bool propertyExist( char *);
	void addOperator(char *);

private:

	Thing Data;
	kNode *firstNode;
	kNode *nextNode;

};

kNode::kNode(char *id) : firstNode(0), nextNode(0){
	
	Data.setId(id);							//creates a new thing object so with the ID and proporties that are passed through the function

}

char *kNode::getId() {

	return Data.getId();

}

void kNode::addProperty(char *prop){

	Data.addProperty(prop);

}

void kNode::addOperator(char *op){

	Data.addOperator(op);

}


void kNode::setId(char *id){

	Data.setId(id);

}

bool kNode::propertyExist(char *prop){

	return Data.propertyExist(prop);

}
#endif 


Error 1 error C2143: syntax error : missing ';' before '*' c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 22
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 22
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 22
Error 4 error C2143: syntax error : missing ';' before '*' c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 23
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 23
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 23
Error 7 error C2143: syntax error : missing ';' before '*' c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 24
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 24
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 24
Error 11 error C2614: 'kList' : illegal member initialization: 'lastNode' is not a base or member c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 29
Error 12 error C2614: 'kList' : illegal member initialization: 'firstNode' is not a base or member c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 29
Error 13 error C2065: 'kNode' : undeclared identifier c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 39
Error 14 error C2065: 'currentNode' : undeclared identifier c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 39
Error 15 error C2065: 'firstNode' : undeclared identifier c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 39
Error 16 error C2065: 'kNode' : undeclared identifier c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 40
Error 17 error C2065: 'tempNode' : undeclared identifier c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 40
Error 18 error C2065: 'currentNode' : undeclared identifier c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 42
Error 19 error C1903: unable to recover from previous error(s); stopping compilation c:\documents and settings\brett\my documents\visual studio 2008\projects\jarvis stage1\jarvis stage1\klist.h 42

all errors occur in kList.h
Last edited on
Topic archived. No new replies allowed.