22

I have following code:

Tools::Logger.Log(string(GetLastError()), Error);

GetLastError() returns a DWORD a numeric value, but the constructor of std::string doesn't accept a DWORD.

What can I do?

Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • (Moved from non-answer) Read [The String Formatters of Manor Farm](http://www.gotw.ca/publications/mill19.htm) by Herb Sutter. It's a great comparison of the different ways to convert data to strings, including std::stringstream, Boost::lexical_cast, sprintf, snprintf, and std::strstream. – Fred Larson Oct 31 '14 at 20:55

8 Answers8

38

You want to read up on ostringstream:

#include <sstream>
#include <string>

int main()
{
   std::ostringstream stream;
   int i = 5;
   stream << i;
   std::string str = stream.str();
} 
Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • 1
    that's funny... who would downvote this? I wouldn't think this would be controversial :) – Doug T. May 22 '09 at 13:48
  • 2
    If you forget #include you will get a strange error message: "error: aggregate ‘std::ostringstream ...’ has incomplete type and cannot be defined". Simple fix. – Jared Updike Oct 05 '10 at 03:05
  • 1
    Most answers have got exactly one downvote. Could it be somebody downvoted every answer but their own? Unfortunately there is more than one suspect :) – David Sykes Feb 24 '11 at 15:57
  • @David They'd have to be one of the high-rep folks. I wonder where all that Jon Skeet rep goes... – Mateen Ulhaq Jun 06 '11 at 05:19
22

You want to convert the number to a string:

std::ostringstream os;
os << GetLastError();
Log(os.str(), Error);

Or boost::lexical_cast:

Log(boost::lexical_cast<std::string>(GetLastError()), Error);
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
20

Since C++11

std::to_string() with overloads for int, long, long long, unsigned int, unsigned long, unsigned long long, float, double, and long double.

#include <string>

auto i = 1337;
auto si = std::to_string(i); // "1337"
auto f = .1234f;
auto sf = std::to_string(f); // "0.123400"

Yes, I'm a fan of auto.

To use your example:

Tools::Logger.Log(std::to_string(GetLastError()), Error);
Max Truxa
  • 3,308
  • 25
  • 38
9

Use Boost's lexical_cast for simple cases such as the above:

Tools::Logger.Log(lexical_cast<string>(GetLastError()), Error);
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

You can use STLSoft's winstl::int_to_string(), as follows:

Tools::Logger.Log(winstl::int_to_string(GetLastError()), Error);

Also, if you want to lookup the string form of the error code, you can use STLSoft's winstl::error_desc.

There were a bunch of articles in Dr Dobb's about this a few years ago: parts one, two, three, four. Goes into the subject in great detail, particularly about performance.

dcw
  • 3,481
  • 2
  • 22
  • 30
1

Use std::stringstream.

std::stringstream errorStream;
errorStream << GetLastError();
Tools::Logger.Log(errorStream.str(), Error);
Benoît
  • 16,798
  • 8
  • 46
  • 66
0

what i normally do is:

std::ostringstream oss;
oss << GetLastError() << " :: " << Error << std::endl;
Tools::Logger.Log(oss.str()); // or whatever interface is for logging
PiNoYBoY82
  • 1,618
  • 2
  • 14
  • 17
0

As all guys here suggested, implementation will use stringstream.
In my current project we created function

template <typename T>
std::string util::str::build( const T& value );

to create string from any source.

So in our project it would be

Tools::Logger.Log( util::str::build(GetLastError()) );

Such usage of streams in the suggested way wouldn't pass my review unless someone wrap it.

Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
  • Do you like how it looks? Can you specify it for work with doubles. For work with your own type which can't be used with the stream? – Mykola Golubyev May 22 '09 at 09:12