Undefined references

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

Here are my files:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "exercise11.h"

int main()
{
	linkedList<int> myList( 1 );

	myList.addElement( 5 );

	myList.printList();

	return 0;
}


exercise11.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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


exercise11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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 );
}

Last edited on
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
Topic archived. No new replies allowed.