Before posting this, as I know It's may be a common problem, I've already read posts on StackOverflow and read about templates but I'm still not able to solve my problem.
I have a class Tool with one function inside.
.h file
class Tools{
public:
template<typename T>
static std::string tostr(const T& t);
};
.cpp file
template<typename T>
std::string Tools::tostr(const T& t) {
ostringstream os;
os<<t;
string res= os.str();
return res;
}
The purpose of this function is to transform a number into a string (I know that the c++ 11 has already a function who does that but I would like to learn and to be more familiar with the concept)
In a another class, I use the function as the following:
string resStr= Tools::tostr<string>("7.12341");
But I got this error:
error: no matching function for call to 'Tools::tostr(float)'
Then I also tried this line:
string resStr= Tools::tostr("7.12341");
And I got also an error :
error: undefined reference to `std::string Tools::tostr(float const&)'
I don't understand because I did the following verifications:
- I defined static in the .h file the function
- I put the
template<typename T>
to define the template (I also tried withtemplate<class T>
) - The return type string is well defined
- The class Tool.h is well included in the class when I call the function
Any ideas ?
Thanks you all.