From: Larry Lindstrom on 27 Jan 2007 00:56 Hi Folks: Developing with VC6, on XP, Win32, no MFC. I'm trying to convert a double precision float value to a text string. I'll need two versions, for and TCHAR and ASCII char buffers. _ecvt_s and _fcvt_s generate "undeclared identifier" errors. So I'm trying to use: using namespace std; tstring *double_to_string(double double_value, string *string_ptr, int chars_rt_of_decimal) { ostringstream out_stream; if(string_ptr != NULL) { out_stream << fixed << showpoint << ios::precision(chars_rt_of_decimal) << double_value; *string_ptr = out_stream.str(); } return string_ptr; } This is eliciting an "illegal call of non-static member function" error. I've tried replacing "ios::precision()" with "std::setprecision()" This results in "'setprecision' : is not a member of 'std'". Even though, typing "std::" does the auto-complete of setprecision. suggestions? Thanks Larry
From: Jerry Coffin on 27 Jan 2007 11:02 In article <E5WdnV3ah8QOdCfYnZ2dnUVZ_rqhnZ2d(a)comcast.com>, nobody(a)aracnet.com says... > Hi Folks: > > Developing with VC6, on XP, Win32, no MFC. > > I'm trying to convert a double precision float value to a text > string. First of all, VC6 has some flakiness when it comes to using directives, so I removed that, and explicitly qualified the names. Second, I didn't see a whole lot of reason to pass in the string being affected -- most modern compilers can optimize returning values objects so the result of the function is created directly where it's assigned instead of creating temporaries and such (and the standard specifically allows the compiler to do this, giving permission for ctors and/or dtors to be elided, even if they have visible side-effects). That produces a function that's clearly shorter, and IMO easier to follow as well. If you find that returning a string instead of modifying a string that's passed in makes a huge difference in speed, you can obviously change that back, but I have a hard time imagining a program that would need to do this in an inner loop or anything like that, so I'd tend to leave it alone until or unless a profiler showed it really was a bottleneck. #include <sstream> #include <iostream> #include <iomanip> std::string double_to_string(double double_value, int chars_rt_of_decimal) { std::ostringstream out_stream; out_stream << std::fixed << std::showpoint << std::setprecision(chars_rt_of_decimal) << double_value; return out_stream.str(); } int main() { double x = 1.23456; std::string y = double_to_string(x, 2); std::cout << y << "\n"; return 0; } As far as the problem you were having: my immediate guess is that it was a problem with not including the right headers, though you didn't say what headers you had included, so it's hard to say for sure. -- Later, Jerry. The universe is a figment of its own imagination.
From: Larry Lindstrom on 27 Jan 2007 16:26 Jerry Coffin wrote: < Snip > Thanks Jerry: I was doing a Windows search for "precision" and "setprecision" in all of the ".h" files on the system. That doesn't do much for C++ STL headers. I appreciate your help. Larry
|
Pages: 1 Prev: Custom MessageBox() icon? Next: Timers in Console Applications |