Compile Error/Class Definition

JC0133

Senior member
Nov 2, 2010
201
1
76
I am receiving a compile error "'IntNode::ptrSlot' uses undefined class 'LinkedList1'"
for the code that is in bold below LinkedList1 ptrSlot; . When I remove that it works fine. I tried declaring the classes above but this does not fix the issue?

I am not sure what is wrong?

Code:
//My example

#include <iostream>
using namespace std;

class IntNode;
class LinkedList1;


class IntNode {
    public:
        IntNode(){}
        IntNode(int theData, IntNode* theLink) : data(theData),link(theLink) {}
        IntNode* getLink() const {return link; }
        int getData() const {return data; }
        void setData(int theData) {data = theData; }
        void setLink(IntNode* pointer) {link = pointer; }
        //void headInsert(int theData1) { ptrSlot.headInsert(theData1); }
        //void remove(int theData1) { ptrSlot.remove(theData1); }
        //void deleteHeadNode() { ptrSlot.deleteHeadNode(); }

private:
    int data;
    IntNode *link;
    [B]LinkedList1 ptrSlot;[/B]

};typedef IntNode* IntNodePtr;

class LinkedList1 {
    public:
        LinkedList1() {head = NULL;}
        void headInsert(int theData);
        void insert(IntNodePtr afterMe, int theData);
        void deleteHeadNode();
        void remove(int theData);
        void output();
    private:
        IntNodePtr head;
};

int main() {
    char ans;
    int tableSize=0;

    cout<<"Please enter in the table size: ";
    cin >> tableSize;
    LinkedList1* first;

    first = new LinkedList1[tableSize];


    do {
        first[0].headInsert(2);
        first[0].headInsert(3);
        first[0].headInsert(4);
        first[0].headInsert(5);
        first[0].headInsert(6);
        first[0].output();
        first[0].deleteHeadNode();
        cout<<endl;
        first[0].output();
        first[0].remove(3);
        cout<<endl;
        first[0].output();

        cout << "Would you like to start over again(y = yes/n = no)? ";
        cin >> ans;

    } while (ans != 'n');

    return 0;
}//end of main


void LinkedList1::headInsert(int theData) {
    head = new IntNode(theData, head);
}//end of headInsert

void LinkedList1::insert(IntNodePtr afterMe, int theData) {
    afterMe->setLink(new IntNode(theData, afterMe->getLink()));
}//end of insert

void LinkedList1::deleteHeadNode() {
    if (head != NULL) {
        head = head->getLink();
    }//end of if statement
    else {
        cout<<"You are at the first node already"<<endl;
    }//end of else
}//end of deleteHeadNode

void LinkedList1::remove(int theData) {
    IntNodePtr before,discard,test; 
    before = head;
    discard = head;
    test = head;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    if (before == NULL) {
        cout<<"List is empty"<<endl;
    }
    else {
        while(discard->getData() != theData && discard->getLink() != NULL) {
            count1++;
            discard = discard->getLink();
        }//end of while loop

        int i = count1 - 1;

        while (count2 != i) {
            before = before->getLink();
            count2++;
        }//end of while loop

        while(test->getLink() != NULL) {
            test = test->getLink();
            count3++;
        }

        if(count3 == count1) {
            cout<<"Value is not in the list"<<endl;
        }
    }

    before->setLink(discard->getLink());
}//end of remove function

void LinkedList1::output() {
    IntNodePtr position = head;

    if(position == NULL) {
        cout<<"The list is empty"<<endl;
    }
    else {
        while(position != NULL) {
            cout<<"The values of the list are "<<position->getData()<<endl;
            position = position->getLink();
        }//end of while loop
    }
}//end of output list
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
A forward declaration only says to the compiler "a class of this name will exist". Since the compiler sort of trusts your judgement, it will allow you to declare a pointer to the class. If you actually want to instantiate an object (either on the stack, as a member variable, or using "new"), the compiler needs to know how much memory the class uses, therefore it needs the full body of the class declaration.

You have a circular dependency between IntNode and LinkedList1. A circular dependency basically always means that you need to change your design. In this case the most basic question would be, why does a node in a linked list need to "own" a linked list? I suspect that you are confused about how linked lists work. A node is usually just a dumb container for holding data. A linked list class would perform operations on a chain of nodes.

Code:
struct Node
{
  int data;
  Node* next;
};

class LinkedList
{
private:
  Node* head;

public:
  // methods...
};
 
Last edited: