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
tommym121Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
I's most likely that the compiler you are using does not support implementing template functions in separate files, i.e. requires the implementation to be #included. While this separation is permitted by the standard, there still are a bunch of compilers in the wild who can't do that. A less-than-elegant but functional workaround would be to

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

#include "StrUtils.cpp" // <--  I wrote it's not elegant at all.
                                  

Open in new window

0
tommym121Author Commented:
I am using Visual Studio 2012
0
jkrCommented:
OK, in that case, have you added 'StrUtils.cpp' to your project?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

tommym121Author Commented:
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
0
phoffricCommented:
You can define your specific functions right in your .cpp file as follows:
template<typename T> 
std::string ToString(T val)
	{
		static std::ostringstream ostr; //output string stream
		ostr.str(std::string());
		ostr.clear();
		ostr<<val;
		return ostr.str();
	}
template std::string ToString<int>(int); // now your ToString(int) function is defined.
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffricCommented:
I think you will find the following link interesting:
    [35.13] How can I avoid linker errors with my template functions?
0
evilrixSenior Software Engineer (Avast)Commented:
jkr has already identified the problem here: http:#a39569826

The compiler has to see the full template definition at the point of instantiation (the place where it is actually used). This means that if the code using the template is not in the same translation unit (.cpp file) and after the template definition, the compiler cannot see it and so you'll get a linker error.

The simplest (and most common) solution is to just move the template definition into a header and then include that header in each translation unit that needs to use it.

For a more detailed explanation of this issue please see my following blog article.
http://evilrix.com/2012/12/02/separating-c-template-declaration-and-implementation/
0
tommym121Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.