I am writing a program that utilizes a linked list class I am developing and when I compile it no compiler errors are thrown but I get these linker errors:
/tmp/ccv9N16A.o: in function `main':
main.cpp:(.text+0x24): undefined reference to `linkedList<int>::linkedList(int)'
main.cpp:(.text+0x35): undefined reference to `linkedList<int>::addElement(int)'
main.cpp:(.text+0x35): undefined reference to `linkedList<int>::printList()'
collect2: error: 1d returned 1 exit status
In exercise11.cpp, I feel like I pretty clearly defined all of the functions that the linker is unable to recognize. Could anyone tell me what I am doing wrong here?
Note: I compiled it using Unix like this: g++ -std=c++11 -Wall -Wextra -pedantic-errors main.cpp exercise11.cpp -o Exercise11
#ifndef __EXERCISE11_H_INCLUDED
#define __EXERCISE11_H_INCLUDED
/*TO DOS:
* 1) a size function (which would just be a linear time iteration)
* 2) a print function (also O(N) time)
* 3) a search function to see if "x" is in list
(this would also be O(N) worst case time because the problem
* doesn't specify that the list is sorted. Otherwise you could do a O(N log N) time
* merge sort)
* 4) an add function ( O(N) time always.
If there is no pointer to a tail function
* then you absolutely must start from the
head and then keep pointing all the way to the end of the list)
* 5) a remove function ( also worst case
O(N) time since the element to be removed
might be at the very end of the list )
* 6) DONE: constructor*/
template <typename Object>
class linkedList
{
public:
//initializes initial values of the head node
linkedList( Object value );
void addElement( Object element );
void printList();
private:
struct Node
{
Object data;
Node *next;
};
Node *head;
};
#endif
#include <iostream>
#include "exercise11.h"
template <typename Object>
linkedList<Object>::linkedList( Object value )
{
head = new Node;
//initializes the data contained
within node to the value taken by the constructor parameter
head->data = value;
head->next = NULL;
}
template <typename Object>
void linkedList<Object>::addElement( Object element )
{
Node *insertNode = new Node;
//initializes node to be inserted with value taken by the parameter
insertNode->data = element;
insertNode->next = NULL;
Node *currentNode = head;
/*iterates through list until current node
does not point to anything. Have that node point to the inserted node*/
do
{
if( currentNode->next == NULL )
{
currentNode->next = insertNode;
}
else
{
currentNode = currentNode->next;
}
} while( currentNode->next == NULL );
}
template <typename Object>
void linkedList<Object>::printList()
{
Node *currentNode = head;
bool exitList = false;
/*iterates through list printing the data
contained withing each node until the final node is reached*/
do
{
std::cout << currentNode->data << " ";
if( currentNode->next != NULL )
{
currentNode = currentNode->next;
}
else
{
exitList = true;
}
} while( exitList == false );
}
See: 'Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?' https://isocpp.org/wiki/faq/templates
Thank you! I completely forgot that templates make linking more complicated if there is a cpp file to define functions and the class constructors that were declared in a header file. Fixed the issue by just having the declarations and definitions in the same file