visual studio 2019 linker error

i am writing a visual studio console application for a school assignment and i'm having trouble separating the class declarations and their member function definitions into separate files. when i put both the class declarations and the function definitions in the same header file, my program runs perfectly, but when i separate the class declarations and their member functions i get the error LNK2019. i am using visual studio 2019.

when i do it like this i get an error:
this is the header file:

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
#pragma once
#ifndef DVPAREN_H
#define DVPAREN_H

template <typename T> class node;
template <typename T> class stack;
template <typename T> class iterator;

template <typename T>
class node{
public:
	node(T item){ data = item; }
	friend class stack<T>;
	friend class iterator<T>;
private:
	T data = NULL;
	node* next = nullptr;
};

template <typename T>
class stack{
public:
	stack();
	void push(T item);
	void pop();
	int len();
	friend class iterator<T>;
private:
	node<T>* first = 0;
	node<T>* last = nullptr;
	int size = 0;
};

template <typename T>
class iterator{
private:
	node<T>* position = 0;
public:
	iterator();
	void next();
	friend class stack<T>;
};

#endif 

this is the source file:

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
#include "dvparen.h"

template<typename T>
inline stack<T>::stack(){
}

template <typename T>
inline void stack<T>::push(T item){
	node<T>* new_node = new node<T>(item);
	if (size <= 0){
		size = 0;
		first = new_node;
		first->next = nullptr;
		last = first;
	}
	else {
		new_node->next = nullptr;
		last->next = new_node;
		last = new_node;
	}
	size++;
}

template <typename T>
inline void stack<T>::pop(){
	if (size <= 0){
		size--;
		return;
	}
	else{
		iterator<T> iter;
		iter.position = first;
		for (int i = 0; i < size - 1; i++)
			iter.next();
		//we output the item being popped before we pop it
		last = iter.position;
		iter.position->next = nullptr;

	}
	size--;
}

template <typename T>
inline int stack<T>::len(){
	return size;
}

template<typename T>
inline iterator<T>::iterator(){
}

template<typename T>
inline void iterator<T>::next(){
	position = position->next;
}

and this is the main file:

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
#include <iostream>
#include <string>
#include "dvparen.h"

int main(){

	std::string input;
	stack<int> parentheses;

	std::cout << "EVALUATING EXPRESSION PARENTHESIS\n";
	std::cout << "This program will detct unbalanced expression parentheses.\n";
	
	do {
		std::cout << "Enter a string with some parentheses: ";
		std::getline(std::cin, input);
		if (input.size() == 1){
			if (input.at(0) == '(' || input.at(0) == ')'){
				std::cout << "This string does not have balanced parentheses.\n";
				return 0;
			}
		}
	} while (input.find('(') == std::string::npos || 
			 input.find(')') == std::string::npos);

	

	for (unsigned i = 0; i < input.size(); i++){
		if (input.at(i) == '(')
			parentheses.push(1);
		else if (input.at(i) == ')')
			parentheses.pop();
	}

	if (parentheses.len() == 0)
		std::cout << "This string has balanced Parentheses.\n";
	else if (parentheses.len() % 2 != 0)
		std::cout << "This string does not have balanced parentheses.\n";
	return 0;
}

like i said, this configuration gives me several linker errors for the class template functions, but when i put the append the contents of the source file to the end of the header file, the program works as expected. would love some insight as to why this is. thanks in advance.
Last edited on
The definitions of template functions are best put in the header file (not a separate source file) - otherwise the compiler will not know which types to create them for.

Workarounds for this are possible, but messy, and tend to pre-empt what types you might want to use.
Yep. I tried putting template functions in a separate file and got a linker error. But when I switched them to the header file, it was fine.
Topic archived. No new replies allowed.