Hi, I am trying to build a linked list, I have the following, and I get 4 error messages, ListNode, NumberList, and head undeclared, and then syntax error, please help.

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <cstdlib>
//#include "input_data.h"

using namespace std;

class Node {
public:
    int x;
    Node *next;
    Node *prev;
};

class NumberList
{
    private:
    class ListNode
    {
        friend class NumberList;
        double value;               //data portion of the node
        ListNode *next;             //successor pointer, points to the next node

        ListNode(double value1, ListNode *next1 = NULL)     //constructor
        {
            value = value1;
            next = next1;
        }
    };

    ListNode *head;
    public:
    NumberList()                    //constructor, initializes head pointer to NULL
    {head = NULL;}
    void insertNode(double);
    void appendNode(double);
    void deleteNode(double);
    void displayList();
};

//****** appendNode is to add the node to the end of the list *****//

void NumberList::appendNode(double num)
        {
            if (head==NULL)
                head = new ListNode(num);
            else
            {
                ListNode *nodePtr;
                nodePtr = head;
                while (nodePtr->next != NULL)
                {
                    nodePtr = nodePtr->next;
                }
                    nodePtr->next = new ListNode(num);

            }
        }

//****** prints the linked list *****//
void NumberList::displayList()
{
    ListNode *nodePtr;
    nodePtr = head;
    while (nodePtr)
    { 
        cout << nodePtr->value << endl;
        nodePtr = nodePtr->next;
    }
}

//****** Inserts a node *****//
void NumberList::insertNode(double num)
{
    ListNode *nodePtr, *previousNodePtr;
    if (head==NULL || head->value >= num)
    { 
        head = new ListNode(num, head);
    }
    else
    {
        previousNodePtr = head;
        nodePtr = head -> next;
        while (nodePtr != NULL && nodePtr -> value < num)
        {
            previousNodePtr = nodePtr;
            nodePtr = nodePtr -> next;
        }
        previousNodePtr -> next = new ListNode(num, nodePtr);
    }
}

//***** Deletes a node *****//
//**************************//
void NumberList::deleteNode(double num)
{
    ListNode *nodePtr, *previousNodePtr;
    if (!head)
        return;
    if (head->value == num)
    {
        nodePtr = head;
        head = head->next;
        delete nodePtr;
    }
    else
    {
        nodePtr=head;
        while (nodePtr != NULL && nodePtr->value != num)
        {
            previousNodePtr = nodePtr;
            nodePtr = nodePtr->next;
        }
        if(nodePtr)
        {
            previousNodePtr->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

//**** an entry point for execution *****//

int main() {

    NumberList one;
    NumberList two;

    cout << "The following are the elements of Periodic Table:\n" << endl;
    cout << "Atomic    Atomic   Atomic " << endl;
    cout << "number:   symbol:  name:" << endl;
    cout << "------------------------------------------------" << endl;

    fstream myfile;                         //file object
    string input;                               //variable used to read line from file
    myfile.open("input_data.h", ios::in);       //opens file for input, information will be read from the file

    // If we couldn't open the input file stream for reading 

    if (!myfile) 
    { 
        // Print an error and exit 
        cout << "Error, input_data.h could not be opened for reading!" << endl; 

        exit(1); 
    } 

    // While there's still stuff left to read 
    getline(myfile, input);  
    while (!myfile.eof())  
    {      
        // read stuff from the file into a string and print it        
        cout << input << endl;
    }

    ListNode *numberList = NULL;                    //list of numbers
    // Read the file into a linked list
    cout << "The contents of the file are:" << endl;
    while (myfile >> input)
    {
        cout << input << " ";
    //create a node to hold this number
        head = new ListNode (input, head);

    }

    cout << endl;

    one.insertNode(1);
    one.insertNode(2);
    one.insertNode(3);

    one.appendNode(23);     
    one.insertNode(2);
    two.appendNode(90);

    one.displayList();      //prints functions for object "one"
    two.displayList();      //prints functions for object "two"

    one.deleteNode(1);

    cout << endl;
    cout << endl;

    one.displayList();

    cout << endl;

    myfile.close();
    return 0;   

}

Recommended Answers

All 4 Replies

Please Help!
ok, I corrected and now have 1 error message, it says Node, no overloaded arguement takes 2 functions. This is the part I changed:

Node *numberList = NULL;                    //list of numbers
    // Read the file into a linked list
    cout << "The contents of the file are:" << endl;
    while (myfile >> input)
    {
        cout << input << " ";
    //create a node to hold this number
        numberList = new Node (input, numberList);

    }

EDIT: missed how it was before. But your Node itself has a default constructor taking no arguments. I think the problem lies in all the private inner class friend layout that you have. I understand that you may have a requirement for data hiding but I can't quite wrap my head around how to code around that.

Also, please kindly use code tags.

the error is in the constructor, I can't seem figure out how to write it. I need to change "Node" but to what? I previously had as "NumberList" and I had 4 errors.

 error here:   Node *numberList = NULL; //list of numbers
 // Read the file into a linked list
cout << "The contents of the file are:" << endl;
while (myfile >> input)
{
cout << input << " ";
//create a node to hold this number
error here:  numberList = new Node (input, numberList);

} 

I'm still not grasping why you need ListNode and Node.... You probably need to make Node a private class of NumberList and using your NumberList constructor to make new nodes, possibly using a method to add new nodes as necessary.
As it is, node doesn't have a constructor to take in both things. Looking back, Node seems to be one of a doubly linked list and ListNode a node in a singly-linked. I think this might be one of those times to take the requirements and take a step back. See what kind of list you need, which elements constructors should do what. It might be bad design but you can start with everything public and introduce your hiding step by step and change the . operators to getters and setters, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.