Namespace and Temmplate function
-
Friends, I wrote a simple template function, the purpose was to convert a "string" to "number" (just like atoi function). My function is:
namespace CP
{
template < class T >
T ToNum(std::string &strNum)
{
T num=0;std::stringstream strm ( strNum ); strm >> num; return num; }
}
As you can see above, the function
ToNum
is present inside a namespace. In order to use this function in the main program, there are three methods to deal with namespaces. First method is:using namespace CP;
std::string strNum=_T("44");
std::cout << ToNum(strNum)The above method is working fine in which i initially used
using namespace CP
before anything else. There is second method which is also working for me. Instead of usingusing namespace CP
, i am directly calling the functionToNum
as:CP::ToNum(strNum)
But the third method is not working for me what i am doing is:using CP::ToNum;
ToNum<_int64>(strNum);But strangely it is not working and compiler is giving me horrible STL errors like, c:\test\test.cpp(32): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Please tell me what is the problem ?????
-
Friends, I wrote a simple template function, the purpose was to convert a "string" to "number" (just like atoi function). My function is:
namespace CP
{
template < class T >
T ToNum(std::string &strNum)
{
T num=0;std::stringstream strm ( strNum ); strm >> num; return num; }
}
As you can see above, the function
ToNum
is present inside a namespace. In order to use this function in the main program, there are three methods to deal with namespaces. First method is:using namespace CP;
std::string strNum=_T("44");
std::cout << ToNum(strNum)The above method is working fine in which i initially used
using namespace CP
before anything else. There is second method which is also working for me. Instead of usingusing namespace CP
, i am directly calling the functionToNum
as:CP::ToNum(strNum)
But the third method is not working for me what i am doing is:using CP::ToNum;
ToNum<_int64>(strNum);But strangely it is not working and compiler is giving me horrible STL errors like, c:\test\test.cpp(32): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Please tell me what is the problem ?????
Try this #include int main(int argc, char* argv[]) { _int64 a = 10; std::cout << a << std::endl; return 0; } in my system it is not working too. Probably, the broble is in _int64, not in yor function. ================================ Useful links