External Reference Problems

I am trying to create a linked list using multiple files but am getting 2 errors
"main.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::print(void)" (?print@?$LinkedList@H@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::add(int)" (?add@?$LinkedList@H@@QAEXH@Z) referenced in function _main"
I have no idea what I am doing wrong. My syntax seems correct. Please help!

<<<<<<<<< main.cpp >>>>>>>>>>>>>>>>>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <stdlib.h>
#include "LinkedList.h"

using namespace std;

int main() {
	int poop;
	cout << "HELLO!\n";
	LinkedList<int> *list = new LinkedList<int>();
	list->add(10);
	list->add(20);
	list->print();
	cin >> poop;

	return 0;
}
========================================================================
<<<<<<<<<<<<< LinkedList.cpp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <stdlib.h>
#include "LinkedList.h"

template <class T> void LinkedList<T>::add(T value) {
	if (head == NULL) {
		head = new Node(value);
		tail = head;
	}
	else {
		Node *temp = tail;
		tail = new Node(value);
		temp->next = tail;
	}
}

template <class T> void LinkedList<T>::print() {
	Node *curr = head;
	while (curr != NULL) {
		cout << curr->val << " | ";
		curr = curr->next;
	}
}
=======================================================
<<<<<<<<<<<<<<<< LinkedList.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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <class T>
class LinkedList {
	
	public:
		LinkedList() { head = NULL; tail = NULL; }
		//~LinkedList();
		void add(T value);
		//void remove(T value);
		void print();
	
	private:
		struct Node {
			T val;
			Node *next;
			Node(T value) { val = value; next = NULL; }
		};
		Node *head;
		Node *tail;
};

#endif 

=========================================================================
Last edited on
a) please use code tags.
b) how are you compiling? i suspect i know the issue but just to be safe
http://www.cplusplus.com/forum/general/113904/


> LinkedList<int> *list = new LinkedList<int>();
memory leak
I am using Visual Studio to compile
never mind im wrong. its because you need to put the function implementations in the .h file
But it should still work with the implementations being in the LinkedList.cpp file
http://www.learncpp.com/cpp-tutorial/19-header-files/
no, because they are templates
What do I do with the LinkedList.cpp file then? It will give me an error when trying to find the file
remove it from the project
Get rid of it.

The two functions your have there belong in the LinkList.h file. Because you have those two templated functions in a .cpp file, the compiler can't see them when it's compiling main.cpp.
Thank you! I have another problem with visual studio. When I build my project or compile it the console window does not turn on. I have to go into the directory and find the .exe file and run it that way.
that would probably be a setting in your ide
Topic archived. No new replies allowed.