0

I've been searching through documentation and checking the example code given by my class and can't figure out whats wrong here. I'm trying to build a linked list structure and need pointers that can point to nodes, but can't seem to initialize them correctly. The nodes point to each other fine, its the overarching class with first/last/current pointers that is giving me problems.

The following code is giving me a bunch of errors when I try to make pointers of the node class. I get C2238 "unexpected tokens preceding ';'" in line 19, 20, and 21 as well as C2143 "missing ';' before '*'" and C4430 "missing type specifier..." again for lines 19, 20 and 21 (the protected section of linkedList).

template <class Type>
class linkedList
{
public:
    //constructors
    linkedList();

    //functions
    void insertLast(Type data);             //creates a new node at the end of the list with num as its info
    void print();                           //steps through the list printing info at each node
    int length();                           //returns the number of nodes in the list
    void divideMid(linkedList sublist);     //divides the list in half, storing a pointer to the second half in the private linkedList pointer named sublist

    //deconstuctors
    ~linkedList();
protected:
    node *current;                          //temporary pointer
    node *first;                            //pointer to first node in linked list
    node *last;                             //pointer to last node in linked list
    bool firstCreated;                      //keeps track of whether a first node has been assigned
private:
};

template <class Type>
struct node
{
    Type info;
    node<Type> *next;
};

changing the protected section as follows also leaves the C2238 and C4430 error but changes C2143 to "missing ';' before '<'"

    node<Type> *current;                            //temporary pointer
    node<Type> *first;                              //pointer to first node in linked list
    node<Type> *last;                               //pointer to last node in linked list
1
  • Do you know how to write a non-templated linked list? Commented Jul 8, 2017 at 22:48

1 Answer 1

1

You need to make a forward declaration for node before linkedList:

template <class Type>
struct node;

See the working version here please.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.