Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Namespace and Temmplate function

Namespace and Temmplate function

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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 using using namespace CP, i am directly calling the function ToNum 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 ?????

    B 1 Reply Last reply
    0
    • L Lost User

      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 using using namespace CP, i am directly calling the function ToNum 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 ?????

      B Offline
      B Offline
      Brigsoft
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups