Random Compile Error

Hello, I am getting this error while using the latest version of Code::Blocks.

error: expected constructor, destructor, or type conversion before '::' token
Here is the offending code. The error occurs at line 33.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

template <typename T>
class Node                              //Node Class
{
    public:
        T data;                         //Data container
        Node *link;                     //points to the next element in the list
};

template <typename T>
class LinkedList                        //Singly Linked List ADT
{
    private:
        Node<T> *ptr, *headptr, *p1, *p2;

    public:
        LinkedList();                   //default constructor
        ~LinkedList();                  //default destructor
        bool listIsEmpty();             //check to see if list is empty
};

template <typename T>
LinkedList<T>::LinkedList()             //Linked List Constructor initiates everything to NULL
{
    headptr = NULL;
        ptr = NULL;
         p1 = NULL;
         p2 = NULL;
}

template <typename T>
bool LinkedList<T>::listIsEmpty()
{
    if(headptr->link == NULL)
        return true;
    else
        return false;
}

template <typename T>
~LinkedList<T>::LinkedList()                //Linked List Destructor clears and deallocates everything in the list
{
    ptr = headptr;                          //Start at the very beginning of the list
    if(listIsEmpty())                             //Check to see if the list is empty
    {
        //nothing contained in the list
    }

    else                                //List has something in it
    {
        while(headptr->link != NULL)    //loop to delete every item in the list
        {
            ptr = headptr->link;        //increment pointer
            headptr->link = ptr->link;  //skip over ptr's element
            delete ptr;                 //delete ptr
        }
    }
}

#endif // LIST_H_INCLUDED 


I am trying to implement a linked list and my compiler can't make it past this line. I don't know what I am doing wrong.
The error is actually on line 43, not 33.

Wrong:
1
2
template <typename T>
~LinkedList<T>::LinkedList()


Right:
1
2
template <typename T>
LinkedList<T>::~LinkedList()
Doh, I feel like an idiot. Wow that is incredibly simple fix. Thanks!

EDIT: Now it compiles, but the process terminates "with status -1073741819." Does this have something to do with the delete ptr?
Last edited on
Im only a newbie and possibly wrong but I think your constructors and destructors need full blown colons ie : not semi-colons ie ;

LinkedList();
~LinkedList();

you have semi-colons.
No, that isn't it.

I will say that your destructor implementation is wrong; it will not clean up the entire list, but will rather
leave 1 node around.

I will also say that your LinkedList class is not assignable or copyable, but you haven't disabled these
operations (by declaring the copy constructor and assignment operator private and leaving them unimplemented).
If you do use these operations, your program *will* crash, but you haven't provided all your code to know.

I figured out the problem. Headptr and ptr were never allocated using 'new' so the program would always fail to 'delete' them.
Topic archived. No new replies allowed.