I get a linker error when I try to create an executable from the following code. I get the impression I need to place a few “typenames” around or make some forward declarations; I’ve tried a few combinations but none worked.
template<typename T>
class enabled
{
private:
T type_;
friend const T& typeof(const enabled<T>& obj); // Offending line
};
template<typename T>
const T& typeof(const enabled<T>& obj) {
return obj.type_;
}
int main()
{
enabled<std::string> en;
std::cout << typeof(en);
std::cin.clear(), std::cin.get();
return 0;
}
1>main.obj : error LNK2001: unresolved external symbol “class std::string const& __cdecl typeof(class enabled<class std::string> const&)”
By forward declaring and specifying that the function is templated