c++ - Getting a roll-your-own patch for std::to_string() to work properly with doubles on an old compiler -


this question has answer here:

i'm patching security flaws in 15-20 year old system hasn't been maintained 10 years. such, of "patches" work arounds , kludges because real solution erode integrity of working (if insecure) system. 1 of "patches" applied related old compiler not being able find to_string() within std, works pretty well, not perfectly.

somewhere in these thousands of lines of code, try convert double using patch::to_string(), , fails compile following error:

    g++  -o main -wall -g -wextra -pedantic main.o veracodefunctions.o main.o: in function `main': /home/(omited privacy)/2_test/main.cpp:8: undefined reference `std::basic_string<char, std::char_traits<char>, std::allocator<char> > patch::to_string<double>(double const&)' collect2: ld returned 1 exit status *** error code 1 clearmake: error: build script failed "main" 

below original implementation of patch used, got the above stack overflow post, , should work:

#include <string> #include <sstream>  namespace patch {     template < typename t > std::string to_string( const t& n )     {         std::ostringstream stm ;         stm << n ;         return stm.str() ;     } }  #include <iostream>  int main() {     std::cout << patch::to_string(1234) << '\n' << patch::to_string(1234.56) << '\n' ; } 

the patch cminor has main() @ bottom of supposed illustrate works both int , double values, reason mine works int, , not double. here implementation:

patch.h:

8 #include <string> ... 25 namespace patch 26 { 27    template < typename t > std::string to_string( const t& n ); 28 } 

patch.cpp:

6 #include <string> 7 #include <sstream> 8 #include <stdexcept> 9 #include "patch.h" ... 41 namespace patch 42 { 43    template < typename t > std::string to_string( const t& n ) 44    { 45       std::ostringstream stm; 46       stm << n; 47       return stm.str(); 48    } 49 } 

main.cpp:

2 #include <iostream> 3 #include "patch.h" 4  5 int main () { 6  7    std::cout << "\"12345\" string is: " << patch::to_string(12345) << std::endl; 8    std::cout << "\"12.345\" string is: " << patch::to_string(12.345) << std::endl; 9    return 0; 10  11 } 

and don't have scroll, here compile error again:

    g++  -o main -wall -g -wextra -pedantic main.o veracodefunctions.o main.o: in function `main': /home/(omited privacy)/2_test/main.cpp:8: undefined reference `std::basic_string<char, std::char_traits<char>, std::allocator<char> > patch::to_string<double>(double const&)' collect2: ld returned 1 exit status *** error code 1 clearmake: error: build script failed "main" 

any insight appreciated!

you declared to_string() in header, , defined in cpp files. while normal way non-template functions, incorrect templates. need place definition (the actual implementation) in .h header file, not in .cpp file.

patch.h:

#include <string> #include <sstream> #include <stdexcept>  namespace patch {      template < typename t > std::string to_string( const t& n )     {        std::ostringstream stm;        stm << n;        return stm.str();     } } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -