I'm patching up security flaws in a 15-20 year old system that hasn't been maintained for 10 years. As such, most of my "patches" are really work arounds and kludges because a real solution would erode the integrity of a working (if insecure) system. One of the "patches" I applied was related to my old compiler not being able to find to_string()
within std
, which works pretty well, but not perfectly.
Somewhere in these thousands of lines of code, I try to convert a double
using patch::to_string()
, and it fails to compile with the following error:
g++ -o main -Wall -g -Wextra -pedantic main.o veracodefunctions.o
main.o: In function `main':
/home/(omited for privacy)/2_Test/main.cpp:8: undefined reference to `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 for "main"
Below is the original implementation of the patch I used, which I got from the above Stack Overflow post, and 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 by cMinor has this main()
at the bottom of it that is supposed to illustrate that it works with both int
and double
values, but for some reason mine only works with int
, and not with double
. Here is my 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\" to string is: " << patch::to_string(12345) << std::endl;
8 std::cout << "\"12.345\" to string is: " << patch::to_string(12.345) << std::endl;
9 return 0;
10
11 }
And just so you don't have to scroll, here is the compile error again:
g++ -o main -Wall -g -Wextra -pedantic main.o veracodefunctions.o
main.o: In function `main':
/home/(omited for privacy)/2_Test/main.cpp:8: undefined reference to `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 for "main"
Any insight would be much appreciated!