-2

Every time I have a class in a header file and I'm using the class in the source file, I get the same error. Doesn't matter which class or which project it is.

I am trying to insert a new node onto the head of a linked list data structure.

Right now I have a pretty simple header file main.h:

namespace linkedlistofclasses {
    class Node {
        public:
            Node();
            Node(int value, Node *next);
            //Constructor to initialize a node

            int getData() const;
            //Retrieve value for this node

            Node *getLink() const;
            //Retrieve next Node in the list

            void setData(int value);
            //Use to modify the value stored in the list

            void setLink(Node *next);
            //Use to change the reference to the next node

        private:
            int data;
            Node *link;
        };

    typedef Node* NodePtr;
}

My source file main.cpp looks like this:

#include <iostream>
#include "main.h"

using namespace std;
using namespace linkedlistofclasses;

void head_insert(NodePtr &head, int the_number) {

    NodePtr temp_ptr;
    //The constructor sets temp_ptr->link to head and
    //sets the data value to the_number

    temp_ptr = new Node(the_number, head);
    head = temp_ptr;
}

int main() {

    NodePtr head, temp;

    //Create a list of nodes 4->3->2->1->0
    head = new Node(0, NULL);

    for (int i = 1; i < 5; i++) {
        head_insert(head, i);
    }

    //Iterate through the list and display each value 
    temp = head;
    while (temp !=NULL) {
        cout << temp->getData() << endl;
        temp = temp->getLink();
    }

    //Delete all nodes in the list before exiting
    //the program.
    temp = head;
    while (temp !=NULL) {
        NodePtr nodeToDelete = temp;
        temp = temp->getLink();
        delete nodeToDelete;
    }

    return 0;
}

My problem is that I get these compilation errors:

Undefined symbols for architecture x86_64:
  "linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
      head_insert(linkedlistofclasses::Node*&, int) in main.o
      _main in main.o
  "linkedlistofclasses::Node::getData() const", referenced from:
      _main in main.o
  "linkedlistofclasses::Node::getLink() const", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I run the code without a using a class, writing everything in the source file main.cpp, there is no problem. But no matter how I write a class, I always get some variant of this error.

Daniel Beck
  • 16,972
  • 5
  • 29
  • 49
Drudoo
  • 33
  • 1
  • 7

1 Answers1

2

You are merely declaring the following class methods:

Node(int value, Node *next);
//Constructor to initialize a node

int getData() const;
//Retrieve value for this node

Node *getLink() const;
//Retrieve next Node in the list

void setData(int value);
//Use to modify the value stored in the list

void setLink(Node *next);
//Use to change the reference to the next node

Which allows those who include main.h to see the existence of linkedlistofclasses::Node, as well as its public members. This way, you are able to call them, for example, from the main() function.

However, as they are simply declared and not defined, this raises issues later on:

**Undefined symbols for architecture x86_64:
"linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
  head_insert(linkedlistofclasses::Node*&, int) in main.o
  _main in main.o
"linkedlistofclasses::Node::getData() const", referenced from:
  _main in main.o
"linkedlistofclasses::Node::getLink() const", referenced from:
  _main in main.o**

Those error messages you got are occurring because the linker does not know the region in the internal representation of the program where the code associated to those method names resides. And it can't be found because you did not define it in the first place! Consider the situation: what sense does it make to call a function whose code is non-existent?

May I suggest that you create a Node.h and Node.cpp files, the first with declarations and the second with definitions (again, differences between the two concepts), and then #include Node.h from main.c?

In the current situation, you will notice that if additionally you invoke any of these:

void setData(int value);
//Use to modify the value stored in the list
void setLink(Node *next);
//Use to change the reference to the next node

Their names will be added to the message errors you're already receiving! Also, to improve legibility, may I recommend changing the namespace to LinkedListOfClasses?

EDIT: regarding your comment about what you posted in pastebin:

// Node.h
namespace LinkedListOfClasses {
  class Node {

  public:
      Node();
      Node(int value, Node *next);
      int getData() const;
      Node *getLink() const;
      void setData(int value);
      void setLink(Node *next);
  private:
      int data;
      Node *link;
  };
  typedef Node* NodePtr;
}

The code above contains the declarations of the methods you mentioned in your comment. Regarding the missing definition of the following methods declared in the Node class:

int getData() const;
Node *getLink() const;

You want to include their definitions in your Node**.cpp** file, not the .h! The Node.cpp would then look like this:

// Node.cpp
#include "Node.h"

using namespace LinkedListOfClasses;

void head_insert(NodePtr &head, int the_number) {

    NodePtr temp_ptr;
    temp_ptr = new Node(the_number, head);
    head = temp_ptr; 
}

Node::Node(int value, Node *next) {  
}

void Node::setData(int value) {
}

void Node::setLink(Node *next) {
}

int Node::getData() const {
  // getData definition was missing, now it's defined in Node.cpp!
}
Node *Node::getLink() const {
  // getLink definition was missing, now it's defined in Node.cpp!
}

Hope I managed to be of some help.

Community
  • 1
  • 1
Blitzkoder
  • 1,638
  • 3
  • 14
  • 28
  • I think i understand what you are saying, but i am still not sure what how to define "int getData() const;" and "Node *getLink() const;" in my Node.h. Here is a pastebin of by three files: http://pastebin.com/xAmgqD2N – Drudoo Oct 13 '12 at 13:18
  • Thank you so much! Both your code and the link to declared/defined, helped a lot. It is working now. I will try make a few more classes to get a hang of it! =) – Drudoo Oct 13 '12 at 14:04