Undefined Reference to
- Usually headers guards are for header files (i.e.,
.h
) not for source files ( i.e.,.cpp
). - Include the necessary standard headers and namespaces in source files.
LinearNode.h:
#ifndef LINEARNODE_H
#define LINEARNODE_H
class LinearNode
{
// .....
};
#endif
LinearNode.cpp:
#include "LinearNode.h"
#include <iostream>
using namespace std;
// And now the definitions
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class LinearNode; // Forward Declaration
class LinkedList
{
// ...
};
#endif
LinkedList.cpp
#include "LinearNode.h"
#include "LinkedList.h"
#include <iostream>
using namespace std;
// Definitions
test.cpp is source file is fine. Note that header files are never compiled. Assuming all the files are in a single folder -
g++ LinearNode.cpp LinkedList.cpp test.cpp -o exe.out
I was getting this error because my cpp files was not added in the CMakeLists.txt file