1

I just wrote a simple program that creates a linked list. When I wrote the entire program in just one file (main.cpp) the program compiled and ran fine. However, when I separated the program into a header file, an implementation file, and main, it doesn't work. I get the error "undefined reference to List::AddNode(int). I don't understand why...

header file

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include <iostream>
using namespace std;

class List
{
 public:
     List();
     void AddNode(int addData);
     void DeleteNode(int deldata);
     void PrintList();


 private:
    struct Node
    {
        int data;
        Node* next;

    };

    Node* head;
    Node* curr;
    Node* temp;
};


#endif // LINKEDLIST_H_INCLUDED

List.cpp

#include <cstdlib>
#include <iostream>
#include "List.h"
using namespace std;

List::List()
{
    head = nullptr;
    curr = nullptr;
    temp = nullptr;
}

void List::AddNode(int addData)
{
    nodePtr n = new Node;
    n->next = nullptr;
    n->data = addData;

    if(head !=nullptr)
    {
        curr = head;
        while(curr->next!=nullptr)
        {
            curr = curr->next; //This while loop advances the curr pointer to the next node in the list
        }
        curr->next = n; //now the last node in the list points to the new node we created. So our new node is now the last node
    }

    else
    {
        head = n;
    }

}

void List::DeleteNode(int delData)
{
    Node* delPtr = nullptr; //created a deletion pointer and set it equal to nothing (it's not pointing to anything)
    temp = head;
    curr = head;
    while(curr != nullptr && curr->data !=delData)
    {
       temp = curr;
       curr = curr->next;
    }
    if(curr==nullptr)
    {
        cout << delData << " was not in the list \n"
        delete delPtr;
    }
    else
    {
        delPtr = curr;
        curr = curr->next;
        temp->next = curr;
        delete delPtr;
        cout << "the value " << delData << "was deleted \n";
    }

}

void List::PrintList()
{
    head = curr;
    while(curr!=nullptr)
    {
        cout<<curr->data<<endl;
        curr = curr->next;
    }
}

main.cpp

#include <iostream>
#include <cstdlib>
#include "List.h"


using namespace std;

int main()
{
    List crisa;
    crisa.AddNode(5);
}

I get the error in the main.cpp file when I try to call the function AddNode. I don't know why, could it be a compiler issue?

user3341399
  • 81
  • 2
  • 4
  • 8
  • 3
    You need to compile List.cpp also and link the resulting object file to create the executable. – R Sahu Mar 10 '15 at 17:16
  • 3
    Unrelated, what earthly reason could you possibly have for `using namespace std;` in a header that uses exactly *none* of the standard library facilities ? (and its [usually a bad idea](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) even if that weren't true). – WhozCraig Mar 10 '15 at 17:20
  • 1
    In case you didn't click on that link provided by WhozCraig, there are a number of C++ programmers who would go postal on you for having "using namespace std" in a header file. – David Hammen Mar 10 '15 at 17:22
  • Unrelated, what earthly reason could you possibly have for `#include ` in a header that uses exactly *none* of the capabilities provided by `iostream`? Gratuitous #includes will get you in deep trouble one day. – David Hammen Mar 10 '15 at 17:25
  • Because I am going to be using the standard library soon. This isn't the completed program. And thank you. – user3341399 Mar 10 '15 at 17:27
  • I also don't know why when I call the PrintList function the first node of the list will never print out. For example, if I call AddNode three different times, the list should constist of 3 integers. However, PrintList will only print out the last 2? – user3341399 Mar 10 '15 at 17:30
  • @WhozCraig thank you for the link, I was unaware of all of this until now – user3341399 Mar 10 '15 at 17:32

0 Answers0