-5

I have a function which is defined as

void writeSite(string& site, const string& contentType);

Now I am building my string with the std::ostringstream function.

After finishing the string I want to call the writeSite function. But I get the following error:

no matching function for call to ‘writeSite(std::basic_ostringstream<char>::__string_type, const char [17])’
   writeSite(body.str(), "application/json");

I can solve the problem if I first save the ostringstream to a new std::string variable and then call the writeSite function.

But I was wondering, if there is a better option?

  std::ostringstream body;

  body << "{";

  // some more string building

  body << "}";

  std::string sbody = body.str();

  writeSite(sbody, "application/json");
Pascal
  • 2,175
  • 4
  • 39
  • 57

1 Answers1

2

When you do

writeSite(body.str(), "application/json");

the string object returned by body.str() is temporary. And non-constant references can not bind to temporary objects.

A simple solution is to make the site argument const just like you have done for the contentType argument (which would otherwise suffer from the same problem).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621