I'm having some difficulties with a template function within a class I'm writing. I've boiled it down to a concise set of examples to make it easy to demonstrate (and this proves that I'm missing or screwing up a fundamental concept here).
Basically, if I define this function outside of the class, or if I set it up as a member function, it works perfectly. These two examples work:
As a member function...
// testing.hpp:
class testing {
public:
template <typename T>
T getVal(std::string szKeyName, T TDefaultVal) {
return TDefaultVal;
}
};
// main.cpp:
#include "testing.hpp"
int main(int argc, const char * argv[]) {
testing x;
std::string st = x.getVal("ConfigKey", "ConfigVal");
std::cout << "Returned value is: " << st << std::endl;
return 0;
}
Stand-alone...
template <typename T>
T getVal(std::string szKeyName, T TDefaultVal) {
return TDefaultVal;
}
int main(int argc, const char * argv[]) {
std::string st = getVal("ConfigKey", "ConfigVal");
std::cout << "Returned value is: " << st << std::endl;
return 0;
}
However, if I separate declaration and implementation of the testing class, it does not work:
// testing.hpp
class testing {
public:
template <typename T>
T getVal(std::string szKeyName, T TDefaultVal);
};
// testing.cpp
#include "testing.hpp"
template <typename T>
T testing::getVal(std::string szKeyName, T TDefaultVal) {
return TDefaultVal;
}
// main.cpp
#include "testing.hpp"
int main(int argc, const char * argv[]) {
testing x;
std::string st = x.getVal("ConfigKey", "ConfigVal");
std::cout << "Returned value is: " << st << std::endl;
return 0;
}
The above fails to compile with:
Undefined symbols for architecture x86_64: "char const* testing::getVal(std::__1::basic_string, std::__1::allocator >, char const*)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
It's really frustrating because I feel like I really should be able to identify the problem, but after a fair bit of tinkering I can't, so I'm hoping someone can square me away here. It's worth noting that regardless of what type I use -- std::string, int, etc. -- I get the linker error. I've also tried calling the function with
std::string st = x.getVal<std::string>("ConfigKey", "ConfigVal");
but this did not help, either. Any help or clarification would be greatly appreciated.
Thank you.