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.
You are merely declaring the following class methods:
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:
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:
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:
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:
You want to include their definitions in your Node**.cpp** file, not the .h! The Node.cpp would then look like this:
Hope I managed to be of some help.