C++ Linked List Class -> header file

George picture George · Oct 7, 2012 · Viewed 17.2k times · Source

Possible Duplicate:
undefined reference to `WinMain@16'

Ive been working on a circular, double linked list. Decided to create a class and use a header. I'm new to C++ so i checked to see how to implement it. I'm not sure if i correctly implemented the struct node within the list.

After compiling the Clist.cpp file, i received this error.

(.text+0xd2): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 0 warnings
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|

--

#ifndef CLIST_H
#define CLIST_H
struct  Node {
        char data;
        Node *prev, *next;
        Node (char d, Node *p, Node *n): data(d), prev(p), next(n)
        {
            if(p) prev->next = this;
            if(n) next->prev = this;
        }
};

class Clist
{
    public:
        Clist(char);
        virtual ~Clist();
        Node *head;     // current node that is being pointed to
        int size;
        bool isEmpty();
        void addNodeBefore(char);           // inserted before head
        void addNodeAfter(char);            // inserted after head
        void addNodeBeforeData(char, Node*);// Same as above, inserted before/after a specific node
        void addNodeAfterData(char, Node*);
        void out(bool);     // Prints the list, true starts from beginning, false starts from end
        void setData(char);
        void setPrev(Node*);
        void setNext(Node*);
        bool findData(char);    // Searches through the list to find the char
        void deleteData(char, bool);

};

#endif // CLIST_H

--

#include "Clist.h"
#include <string.h>
#include <iostream>
using namespace std;

Clist::Clist(char d)
{
    head = new Node(d, NULL, NULL);
    head->next = head->prev = head;
    size = 1;

}
Clist::~Clist()
{

    Node *tmp = this->head;
    Node *temp;
    while(tmp->prev)
        tmp = tmp->prev;
    while(tmp)
    {

        temp = tmp->next;
        delete tmp;
        tmp = temp;
    }
    tmp = temp = NULL;

}
bool Clist::isEmpty()
{   return (this->size == 0);}

void Clist::addNodeBefore(char d)
{
    Node *n = head;
    Node *p = head->prev;

    Node *temp = new Node (d, p, n);
    size++;
    //cout << "added: " << temp->data << "  before: "
    //      << temp->prev->data << "  after: " << temp->next->data << endl;

}
void Clist::addNodeAfter(char d)
{
    Node *n = head->next;
    Node *p = head;

    Node *temp = new Node (d, p, n);
    size++;
    //cout << "added: " << temp->data << "  before: "
    //      << temp->prev->data << "  after: " << temp->next->data << endl;

}
void Clist::out(bool dir)   // True to traverse next, false to traverse prev
{
    if (dir)
    {
        Node *tmp = head;
        do{
            cout << tmp->data;
            tmp = tmp->next;
        }while(tmp != head);
    }else
    {
        Node *tmp = head;
        do{
            cout << tmp->data;
            tmp = tmp->prev;
        }while(tmp != head);
    }
    cout << endl;


}
void Clist::setData(char Data)
{
    this->head->data = Data;
}
void Clist::setPrev(Node* Prev)
{
    this->head->prev = Prev;
}
void Clist::setNext(Node* Next)
{
    this->head->next = Next;
}
bool Clist::findData(char search)
{
    int counter = 0;
    Node *tmp = head;
    while(tmp->next != head)
    {
        if(tmp->data == search)
            counter++;
        tmp = tmp->next;
    }

    if (counter > 0)
    {
        cout << "'" << search << "' was found " << counter << " time(s)" << endl;
        return true;
    }else
    {
        cout << "'" << search << "' was not found" << endl;
        return false;
    }
}

void Clist::deleteData(char search, bool all)   // If true, it will delete all nodes with the same search
{                                               // If false, it will delete the first Node only
    Node *tmp = head;
    while(tmp)
    {
        if(tmp->data == search)
        {
            cout << "Deleting " << search << endl;
            tmp->prev->next = tmp->next;
            tmp->next->prev = tmp->prev;
            if (false)
                return;
        }
        tmp = tmp->next;

    }
}

--

#include <string.h>
#include <iostream>
#include "Clist.h"
using namespace std;



int main(int argc, char* argv[])
{


    char s[]="abcfdefghijklmgnopqrsatuvwxyz";

    Clist *list; // ptr to head of list
    Node *curr; // ptr to current node of the list

    // call constructor and initialize first Node
    list = new Clist(s[0]);

    for(size_t i=1; i < strlen(s); i++) // create the rest of the linked list
        list->addNodeAfter(s[i]);


    list->out(true);
    list->out(false);

    cin.get();


    return 0;
}

Answer

George picture George · Oct 7, 2012

Since im using an IDE (Codeblocks) i tried using the project workspace. Added the class, and it works now.