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:
#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);
elseif (input.at(i) == ')')
parentheses.pop();
}
if (parentheses.len() == 0)
std::cout << "This string has balanced Parentheses.\n";
elseif (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.
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.