Click to See Complete Forum and Search --> : C++ newbie error


toolshed
02-21-2004, 02:21 PM
I am Java programmer that is trying to learn C++ to help me further my Linux skills...I having trouble compiling a simple linkedlist.

I need a pointer to another LinkedList in my node class, but it just not compile it, below is the code if anyone can help. I also attached the file.

LinkedListPW.cpp:16: error: syntax error before `*' token


#include <iostream>
#include <string>
#include "LinkedList.cpp"
using namespace std;

class NodePW {
public:
int count;
string s;
//pointer to the node that is directly next
NodePW* next;

//COMPILE ERROR BELOW COMMENT: here is the problem if I uncomment this line I through an error.It
//must not be able to see the LinkedListPW class below or something

LinkedListPW* nextList;

//THIS DECLARATION ABOVE IS THE COMPILE ERROR

NodePW(string s) {
this->s = s;
}

NodePW() {
next = NULL;

//UNCOMMENT: this line to for your compile

//nextList = new LinkedListPW();
count++;
}
~NodePW() {
if (next != NULL)
delete next;
}
void incCount() {
count++;
}

};

class LinkedListPW {
NodePW *head;
NodePW *tail;
NodePW *current;
int size;
public:
LinkedListPW() {
size = 0;
head = NULL;
tail = NULL;
}
~LinkedListPW() {
delete head;
}
void add(string s);
void del(string s);
void print();
NodePW* next();
void start();
bool hasNext();
bool contains(string s);
NodePW* get(string s);
NodePW* get(int index);
bool isEmpty();
int getSize();
};
void LinkedListPW::add(string s) {
NodePW *temp;
temp = new NodePW;
temp->s = s;

if (size == 0) {
head = temp;
tail = temp;
} else {
tail->next = temp;
tail = temp;
}

/* Our list is now one size bigger. */
size++;
//cout << "LinkedListPW::add(" << temp->s << ")." << endl;
}
bool LinkedListPW::isEmpty() {
if (this->size <= 0) {
return true;
}
return false;

}
int LinkedListPW::getSize() {
return this->size;
}
void LinkedListPW::del(string s) {
NodePW *temp = head, *prev = head;
for (int i=0; i<size; i++) {
if (temp->s == s) {
prev->next = temp->next;
if (head == temp)
head = temp->next;
temp->next = NULL;
delete temp;
i = size;
}
else {
prev = temp;
temp = temp->next;
}
}
size--;
//cout << "LinkedListPW::del(" << s << ")." << endl;
}
void LinkedListPW::print() {
NodePW *temp;
temp = head;
cout << "LinkedListPW::print() output: ";
do {
cout << "[" << temp->s << "] --> ";
temp = temp->next;
} while (temp != NULL);

cout << "NULL" << endl;
}
NodePW* LinkedListPW::next() {
if (current == NULL) {
return NULL;
}
else {
NodePW* temp = current;
current = current->next;
return temp;
}
}
bool LinkedListPW::contains(string s) {
NodePW* temp = head;
while (temp != NULL) {
string tempS = temp->s;
if(tempS == s){
return true;
}
temp = temp->next;
}
return false;
}


void LinkedListPW::start() {
current = head;
}
bool LinkedListPW::hasNext() {
if (current != NULL && size >= 0) {
return true;
}
else {
return false;
}
}
NodePW* LinkedListPW::get(string s) {
NodePW* temp = head;
while (temp != NULL) {
string tempS = temp->s;
if(tempS == s){
return temp;
}
temp = temp->next;
}
return NULL;
}
NodePW* LinkedListPW::get(int index) {
NodePW* temp;
this->start();
for (int i = 0; i <= index; i++) {
if(this->hasNext())
temp = this->next();
else
temp = NULL;
}
if (temp == NULL){
return NULL;
}
return temp;
}


int main(int argc, char *argv[]) {
LinkedListPW list;
list.add("zero");
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");;
list.add("six");

list.print();
//list.del("three");
//list.print();

list.start();
while(list.hasNext()) {
NodePW* node = list.next();
string s = node->s;
cout << s << endl;
}
if (list.contains("tdfdfwo")) {
cout << "Look What I found." <<endl;
}
else {
cout << "Sorry no dice." <<endl;
}

// NodePW* getNodePW = list.get("two");
NodePW* getNodePW = list.get(3);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(6);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(0);
cout << "GetNOde: " << getNodePW->s << endl;

getNodePW = list.get(7);
if (getNodePW == NULL) {
cout << "NULL I AM!" << endl;
}
else{
cout << "GetNOde: " << getNodePW->s << endl;
}


cout << "Done, press enter." << endl;
char c;
cin >> c;
return 0;
}

bwkaz
02-21-2004, 04:30 PM
That's exactly right -- it can't see your LinkedListPW class. The reason that it can't see this class is that C++ classes must be defined before they are referenced, not after.

Since your LinkedListPW class also has a reference to the NodePW class, simply moving LinkedListPW before NodePW isn't an option. What I would do is figure out why you need a reference to the LinkedListPW in your NodePW class (no linked list implementation that I've ever seen has needed to reference any list from its nodes, so by all rights the problematic declaration shouldn't even be needed unless you're doing something very weird...), then refactor your classes so that need goes away. Actually, your NodePW class never uses the nextList member except in its destructor -- so why is that member even there?

If you want to wimp out, though, you can probably add a line like so before the NodePW declaration:

class LinkedListPW;

toolshed
02-23-2004, 12:25 PM
Thanks, that did it.

What exactly does that do, declaring the word class to that member variable?

bwkaz
02-23-2004, 08:06 PM
It tells the compiler that LinkedListPW is a class, whose definition is coming later.

If you were doing anything that depended on the size of objects of that class (like declaring a member variable that wasn't a pointer to a LinkedListPW, but an actual LinkedListPW), then it wouldn't help. But as long as you only need pointers to the object, then it's OK.

But again -- your implementation should not need a pointer to a LinkedListPW inside the NodePW class. You're never touching it except in your constructor (which means that actually, every time you delete a NodePW, you have a memory leak, because the new LinkedListPW that you created in the node's constructor hasn't been deleted).

toolshed
02-23-2004, 11:18 PM
Well, I was just trying to get compiled then add the code.

But the linkedlist i needed kinda looks like a n stacked Z, a kinda zigzag.
Z
Z
Z
Z


thanks for all the info.