It all started when I, being a poor noob, realized that g++ doesn't support itoa.
Long story short, I set out to write up a little library for myself, starting with an itoa replacement, using the stringstream method. Allow me to include my source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
//hr_itoa.cc
//hr_itoa.cc
//itoa replacement
#include <cstdlib>
#include <sstream>
#include <string>
#include "hr_itoa.h"
std::string hr_itoa(int i){
std::stringstream as;
as << i;
return as.str();
}
// hr_itoa.cc
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// hr_itoa.h
#ifndef hr_itoa_h
#define hr_itoa_h
#include <cstdlib>
#include <sstream>
#include <string>
std::string hr_itoa(int i){
std::stringstream a;
std::string stri;
}
#endif
// hr_itoa.h
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// tcli.cc - hr_itoa test client
#include <cstdlib>
#include <iostream>
#include <string>
#include "hr_itoa.h"
int main(){
int numb = 123;
std::string stri;
stri = hr_itoa(numb);
std::cout << stri << std::endl;
return 0;
}
// hr_itoa test client
|
She compiles and seems to link properly, and "runs," but my console output is an assumedly infinite string of whitespace (which piles on so quickly I can't even [Ctrl]-[C] fast enough to see if my "123" string gets output).
Now, for a while there, when I'd comment out the hr_itoa call in tcli.cc, I was getting a (supposedly commonplace) "free() invalid pointer" error and a listing of violated memory addresses and the like. I was, anyhow, but now, commented out or not, std::string stri initialized or not, it doesn't do that any more...
So.. I *have* googled around, and I *have* written a control program to test the core functionality of stringstream on my system:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// wtf.cc
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
int main(){
int i = 132;
std::ostringstream o;
o << i;
std::string stri;
stri = o.str();
std::cout << stri << std::endl;
return 0;
}
// wtf.cc
|
which compiles and runs precisely as it should...
Needless to say, any help given would be quite welcome.
Also, besides the matter at hand, please feel free to tear apart what little code I've written for any other points of poor usage or c++ etiquette.
Thanking you in advance,
HR, ttoh.
P.S.: I'm writing by hand and compiling with g++. And, the output of "$ g++ -v" :
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
/ --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared
/--enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext
/--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu
/--enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic
/--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)
|
'm I missing anything?
EDIT: Prettied up the output of "$ g++ -v"