Thread: can anyone find the problem in my code

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    61

    can anyone find the problem in my code

    With the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    class LinkedList
    {
      public:
    
        LinkedList();
        ~LinkedList();
        void AddNode(char* Name);
        void DeleteList();
    
      protected:
    
        struct node
        {
            char MenuName[20];
            node* nxt;
        };
    
        node* root;
        node* tempnode;
        node* tempnode2;
        node* TraverseList(node* end = NULL);
    
    };
    
    LinkedList::LinkedList()
    {
        root = NULL;
        tempnode = NULL;
        tempnode2 = NULL;
    }
    
    LinkedList::~LinkedList()
    {
        DeleteList();
    }
    
    void LinkedList::AddNode(char* Name)
    {
        tempnode = new node;
        tempnode->nxt = NULL;
        strncpy(tempnode->MenuName, Name, 19);
    
        tempnode2 = TraverseList();
    
        if (tempnode2 == NULL)
            root = tempnode;
        else
            tempnode2->nxt = tempnode;
        return;
    }
    
    void LinkedList::DeleteList()
    {
        while (root != NULL)
        {
            tempnode = TraverseList();
    
            if (tempnode == root)
            {
                root = NULL;
                delete tempnode;
                tempnode = NULL;
            }
            else
            {
                tempnode2 = TraverseList(tempnode);
                tempnode2->nxt = NULL;
                delete tempnode;
                tempnode = NULL;
                tempnode2 = NULL;
            }
        }
        return;
    }
    
    node* LinkedList::TraverseList(node* end = NULL)
    {
        if (root == NULL)
            return NULL;
    
        node* tnode = root;
    
        while (tnode->nxt != end)
        {
            tnode = tnode->nxt;
        }
        return tnode;
    }
    
    int main()
    {
        LinkedList l;
        l.AddNode("Hello");
        l.AddNode("Goodbye");
        l.AddNode("So Long");
        return 0;
    }
    my compiler gives me the error "syntax error before `*'"
    at

    node* LinkedList::TraverseList(node* end = NULL)

    and "syntax error before `*'"
    at

    node* tnode = root;

    Can anyone help?
    Thanks!

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    node is not declared globaly redefine the function as follows.

    LinkedList::node* LinkedList::TraverseList(node* end = NULL)
    {
    if (root == NULL)
    return NULL;

    node* tnode = root;

    while (tnode->nxt != end)
    {
    tnode = tnode->nxt;
    }
    return tnode;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    61

    Thanks

    thanks that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with compiling code with option -O2
    By koushikyou in forum C Programming
    Replies: 16
    Last Post: 01-07-2009, 06:03 AM
  2. Replies: 12
    Last Post: 06-08-2005, 11:23 AM
  3. Odd problem in main(), code won't proceed
    By aciarlillo in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2005, 11:00 PM
  4. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM