Jump to content
  • Advertisement
Sign in to follow this  
thedustbustr

numbers - std::string

This topic is 5389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Having trouble implementing this.

class vector3{float x,y,z;}; //and a lot more
std::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?

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!