Skip to main content
GameDev.net gamedev.net
🔒 Locked

numbers - std::string

Started by thedustbustr Oct 13, 2004 at 7:27 PM 13 replies 1.8k views
Original Post
thedustbustr
thedustbustr
I want to write a wrapper so that I can do this:

class A{float b1, b2;}; A a; a.b1=1f;a.b2=2f;
std::string s; s << a;
or something similarly easy and readable, so that it is the equivalent of

char buf[100];sprintf(buf, "A=(%0.1f %0.1f)",a.b1,a.b2);
Operators don't matter (I could care less if its << or +). How might I accomplish this? Thanks. Dustin
Knaperstekt
Knaperstekt
One way is to overload the << operator.
string& operator << (string& s, const A& a){   stringstream str;   str << "A=(" << a.b1 << " " << a.b2 << ")";   return s = str.str();}

or something like that

/Boris
Knaperstekt
Knaperstekt
Ooops sorry, you didn't want the operator thing... maybee I should read the whole post before replying :-)
sorry 'bout that one.
thedustbustr
thedustbustr
I was unclear - thats exactly what I needed. Thanks :)
jdhardy
jdhardy
boost::format is what you're looking for, I believe.

EDIT: Okay, I see what you wanted. Geez, you wait five seconds...
thedustbustr
thedustbustr
Having trouble implementing this.
class vector3{float x,y,z;}; //and a lot morestd::string& operator << (std::string& s, const vector3& v){   std::stringstream str;   str << "(" << v.x << " " << v.y << " " << v.z << ")";   return s = str.str();}std::string f; f << "f="<<forward;std::string r; r << "r="<<right;std::string u; u << "u="<<up;state.textbox1->DrawStr(0,738,f.c_str());state.textbox1->DrawStr(0,748,r.c_str());state.textbox1->DrawStr(0,758,u.c_str());error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::string'


and 99 more errors. fix plz?
Oluseyi
Oluseyi
operator<< is not defined for insertion into std::strings. Here's what you need:
std::ostream operator << (std::ostream & os, const A & a){  // create stringstream, insert/format, etc here  // insert str() value from stringstream into ostream  return os;}
EvilCrap
EvilCrap
notice that you have defined a single operator.
notice that you are trying to stream a string into another string... s << "a" ... is there an operator for that?
joanusdmentia
joanusdmentia
Why not just use std::stringstream instead of the std::string? You can also include an operator<< that works with a basic_ostream so it can be used for formatted output to any stream (stringstream, fstream, std::cout, etc).

template<typename Elem, typename Traits>std::basic_ostream<Elem,Traits>& operator<<(std::basic_ostream<Elem,Traits>& out, const vector3& in){    return out << "(" << in.x << " " << in.y << " " << in.z << ")";}std::stringstream f; f << "f="<<forward;std::stringstream r; r << "r="<<right;std::stringstream u; u << "u="<<up;state.textbox1->DrawStr(0,738,f.str().c_str());state.textbox1->DrawStr(0,748,r.str().c_str());state.textbox1->DrawStr(0,758,u.str().c_str());


EDIT: Elaborated with basic_ostream stuff

[Edited by - joanusdmentia on October 13, 2004 9:41:48 PM]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
EvilCrap
EvilCrap
howabout adding a member function to the vector class, which will return a string containing data.
thedustbustr
thedustbustr
oluseyi:
std::ostream& operator << (std::ostream& os, const vector3& v){   std::stringstream str;   str << "(" << v.x << " " << v.y << " " << v.z << ")";   os << (s+str.str());   return os;}error C2079: 'str' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'        with        [            _Elem=char,            _Traits=std::char_traits<char>,            _Alloc=std::allocator<char>        ]
I had this error with the original non-ostream attempt as well.

joanusdmentia:
    std::stringstream f; f << "f="<<forward;    state.textbox1->DrawStr(0,738,f.str().c_str());error C2079: 'f' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'        with        [            _Elem=char,            _Traits=std::char_traits<char>,            _Alloc=std::allocator<char>        ]error C2297: '<<' : illegal, right operand has type 'const char [3]'

and also because 'forward' is a vector and needs me to teach it how to format itself.

evilcrap: I did not write the matrix library and I would prefer to leave it untouched

/me is becomg very frustrated
joanusdmentia
joanusdmentia
I elaborated above including an operator<< operator.
Did you #include ? It looks like std::basic_stringstream is being forward declared somewhere but not defined.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
thedustbustr
thedustbustr
Thanks - I appreciate your patience with me. One final question: is there a way to limit to (and force) 4 decimal places (%0.4f)?

Thanks all for walking me through this
Dustin
Knaperstekt
Knaperstekt
Quote:
Original post by thedustbustr
Thanks - I appreciate your patience with me. One final question: is there a way to limit to (and force) 4 decimal places (%0.4f)?

Thanks all for walking me through this
Dustin


It's probably easiest to use the setprecision manipulator:

str << setprecision(4) << a.b1;

this does not force 4 decimals though, it just maximizes the output to 4 decimals. (isn't that what %0.4f in fstream does as well?)

/Boris

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.