Question about strings in C++

What's happening here?
1
2
3
   string temp("("+coef_); // Why does string temp("("+coef_+")*X^" + exp_); not work?
   temp+=")*X^" + exp_; 
   return temp;

(coef_ and exp_ are variables within a class. coef_ is an integer, and exp_ is unsigned)
Thanks in advance!
Last edited on
You can't add integers to a string, use streams:
1
2
3
stringstream temp;
temp << "(" << coef_<< ")*X^" << exp_;
return temp.str();
Last edited on
Well, actually I can.

string temp("(" + coef_ ) ; WORKS

however, i can't use more than one +.

(by the way, i can't get your code to work :-( )
Last edited on
You need to #include<sstream>
BTW "something"+number means pointer+offset which will give you problems
You're right. I made a small program, and "something"+number leads to strange behaviours (Such as not printing anything at all). Plus, I'll try to use sstream

Thanks a lot. I'm marking this as answered.
Last edited on
Topic archived. No new replies allowed.