Link to home
Start Free TrialLog in
Avatar of tommym121
tommym121Flag for Canada

asked on

Calling template function, I get an error

I got this error;
LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl StrUtils::ToString<int>(int)" (??$ToString@H@StrUtils@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function "private: void __thiscall CRuntime::CreateXMLMapFile(wchar_t const *)" (?CreateXMLMapFile@CRuntime@@AAEXPB_W@Z)

In my definition files

"StrUtils.cpp"
#include "stdafx.h"
#include <stdlib.h>
#include <assert.h>
#include <sstream>      // std::istringstream


namespace StrUtils {

template<typename T> string ToString(T val)
	{
		static std::ostringstream ostr; //output string stream
		ostr.str(std::string());
		ostr.clear();
		ostr<<val;
		return ostr.str();
		//string abc;
		//return abc;
	}
}

Open in new window

"StrUtils.hpp"
namespace StrUtils {
	template<typename T>  string ToString(T val);
}

Open in new window


In my calling program,
#include "StrUtils.h"
...
	std::cout <<StrUtils::ToString<int>(123);

Open in new window

Can someone let me know why I get the link error
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tommym121

ASKER

I am using Visual Studio 2012
OK, in that case, have you added 'StrUtils.cpp' to your project?
Yes both .h and .c. I actually have another functions in StrUtil.cpp that I toke out so it will not confuse what I try to ask
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks