Thread: Help w/nodes

  1. #1
    Unregistered
    Guest

    Question Help w/nodes

    I keep getting the same five errors in my code and cannot figure out what I am doing wrong. I am using Visual C++. I can get the same code to compile at school on a linux workstation though.

    I have been looking at this for awhile now and I'm ready to throw my computer out the window. Can someone please tell me what is so obvious that I am not seeing? Thanks in advance.

    The compiler does not like my file node.h. Here are the errors I am getting.

    node.h(10) : error C2146: syntax error : missing ';' before identifier 'data'
    node.h(10) : error C2501: 'string' : missing storage-class or type specifiers
    node.h(10) : error C2501: 'data' : missing storage-class or type specifiers
    node.h(14) : error C2629: unexpected 'class Node ('
    node.h(14) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    Error executing cl.exe.

    main.obj - 5 error(s), 0 warning(s)

    [code]
    //node.h
    #ifndef _NODE_H_
    #define _NODE_H_

    #include "list.h"

    #include <string>
    using std::string;

    class Node {
    friend class List; //doesn't appear to like this code
    private:
    string data;
    Node *next;
    public:
    Node(string item, Node *ptr) {data = item; next = ptr;}

    };

    #endif
    -------------
    //stack.h
    #include "list.h"

    class Stack :private List {
    public:
    string pop() {
    string retval = showHead();
    removeHead();
    return retval;
    }

    void push(string item) {insertAtHead(item);}

    void print() {List::print();}
    };
    ---------
    //list.h
    #ifndef _LIST_H_
    #define _LIST_H_

    #include "node.h"
    #include <string>
    using std::string;

    class List {
    private:
    Node *head;
    Node *tail;
    public:
    List() {head=NULL; tail=NULL;}
    void insertAtHead(string item);
    void insertAtTail(string item);
    string showHead();
    void removeHead();
    void print();
    };
    #endif
    -----------
    //list.cpp
    #include "list.h"
    #include <iostream>

    using std::cout;
    using std::endl;

    void List::print() {
    Node* ptr = head;
    while (ptr != NULL) {
    cout << ptr->data << ", ";
    ptr = ptr->next;
    }
    cout << endl;
    }


    void List::insertAtHead(string item) {
    // insert item at the front of the list pointed to by head
    // assumes that this is a valid list
    head = new Node(item, head);
    if (tail == NULL) {
    tail = head;
    }
    }

    void List::insertAtTail(string item) {
    // insert item at the end of the list pointed to by head
    // assumes that this is a valid list

    if (head == NULL) { // if the list is empty,
    insertAtHead(item); // the beginning is the same as the end
    }
    else {
    tail->next = new Node(item, NULL);
    tail = tail->next;
    }
    }

    void List::removeHead() {
    // decapitate the list pointed to by head
    // assumes that this is a valid list

    if (head != NULL) { // something to delete
    Node *deleteMe = head;
    head = head -> next;
    delete (deleteMe);
    if (head == NULL) { // the list is now empty
    tail = NULL;
    }
    }
    }

    string List::showHead() {
    // show the first value of the list pointed to by head
    // assumes that this is a valid list
    if (head != NULL) {
    return head->data;
    }
    else {
    return "ERROR";
    }
    }
    -------------
    //main.cpp
    #include "list.h"
    #include "stack.h"

    int main() {
    List l1;
    l1.insertAtHead("Hello");
    l1.insertAtHead("Welcome");
    l1.insertAtHead("Greetings");
    l1.print();
    l1.removeHead();
    l1.print();

    Stack s;
    s.push("Hello");
    s.push("Welcome");
    s.push("Greetings");
    s.print();
    s.pop();
    s.print();
    return 0;

    }

    [code/]

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Please fix your code tags, and I'll take a look.
    It's [/code] not [code/]

  3. #3
    Unregistered
    Guest
    Thanks! Here goes . . .

    Code:
    //node.h
    #ifndef _NODE_H_
    #define _NODE_H_
    
    #include "list.h"
    
    #include <string>
    using std::string;
    
    class Node {
    	 friend class List;
     private:
    	 string data;
        Node *next;
     public:
        Node(string item, Node *ptr) {data = item; next = ptr;}
    
    };
    
    #endif
    ---------
    //stack.h
    #include "list.h"
    
    class Stack :private List {
     public:
    	 string pop() {
    	string retval = showHead();
    	removeHead();
    	return retval;
        }
    
    	 void push(string item) {insertAtHead(item);}
    
        void print() {List::print();}
    };
    -------
    //list.h
    #ifndef _LIST_H_
    #define _LIST_H_
    
    #include "node.h"
    #include <string>
    using std::string;
    
    class List {
     private:
    	 Node *head;
    	 Node *tail;
     public:
    	 List() {head=NULL; tail=NULL;}
    	 void insertAtHead(string item);
    	 void insertAtTail(string item);
    	 string showHead();
    	 void removeHead();
    	 void print();
    };
    #endif
    ---------
    //list.cpp
    #include "list.h"
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void List::print() {
        Node* ptr = head;
    	 while (ptr != NULL) {
    	cout << ptr->data << ", ";
    	ptr = ptr->next;
        }
    	 cout << endl;
    }
    
    
    void List::insertAtHead(string item) {
        // insert item at the front of the list pointed to by head
    	 // assumes that this is a valid list
        head = new Node(item, head);
        if (tail == NULL) {
    		tail = head;
        }
    }
    
    void List::insertAtTail(string item) {
        // insert item at the end of the list pointed to by head
    	 // assumes that this is a valid list
    
        if (head == NULL) {     // if the list is empty, 
    		insertAtHead(item); // the beginning is the same as the end
        }
        else {
    		tail->next = new Node(item, NULL);
    		tail = tail->next;
    	 }
    }
    
    void List::removeHead() {
    	 // decapitate the list pointed to by head
    	 // assumes that this is a valid list
    
        if (head != NULL) { // something to delete
    		Node *deleteMe = head;
    		head = head -> next;
    		delete (deleteMe);
    		if (head == NULL) {   // the list is now empty
    			tail = NULL;
    		}
        }
    }
    
    string List::showHead() {
        // show the first value of the list pointed to by head
        // assumes that this is a valid list
    	 if (head != NULL) {
    		return head->data;
    	 }
        else {
    		return "ERROR";
    	 }
    }
    
    
    
    -----------
    //main.cpp
    #include "list.h"
    #include "stack.h"
    
    int main() {
        List l1;
    	 l1.insertAtHead("Hello");
    	 l1.insertAtHead("Welcome");
    	 l1.insertAtHead("Greetings");
    	 l1.print();
    	 l1.removeHead();
    	 l1.print();
    
    	 Stack s;
    	 s.push("Hello");
    	 s.push("Welcome");
        s.push("Greetings");
    	 s.print();
    	 s.pop();
    	 s.print();
    	return 0;
    
    }

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Proceed with throwing the computer. I can't see anything wrong with your code :/ Sorry, I looked it over for about 5 minutes.. but nothing stands out as being wrong.

  5. #5
    Unregistered
    Guest
    lol! Thanks for looking at it. It's very frustrating for this code to compile at school and then I can't get the !@$%^ code to compile at home. I think it's time to work on another project for my class. Thanks again!

Popular pages Recent additions subscribe to a feed