Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Str method of Stringstream will not work. (concatenation of different types) (C++)

I'm brand new to C++ and I am trying to get a simple program up and running.

I'm using the Eclipse IDE ( C++ Version ) on a Windows System.

I am trying to concatenate an output statement, it will combine numbers and strings.

I know in Java, this was automatically done with the System.out.println() method.

What I was able to research was a nice way of implementing this in C++ is to use the string stream method.

#include <iostream>
#include <string>
#include "Person.h"

...

string simpleOutput(){
  stringstream ss;

  int a  = 50; // for testing purposes 
  int b = 60;
  string temp = "Random";
  ss << a << b << temp;
  return string output = ss.str();


}

When I try compiling this code is get the following: "Method "str" could not be resolved.

I have yet to find a solution on any webpage. T Thank you!

like image 773
user2879101 Avatar asked Dec 31 '25 20:12

user2879101


1 Answers

Your primary problem is that you're missing an include:

#include <sstream>

Also, you have a typo in the function, and your return statement is downright crazy. With these fixes, it works:

#include <iostream>
#include <string>
#include <sstream>

string simpleOutput(){
  stringstream ss;

  int a  = 50; // for testing purposes 
  int b = 60;
  string temp = "Random";
  ss << a << b << temp;
  return ss.str();
}

See it live

like image 137
Angew is no longer proud of SO Avatar answered Jan 03 '26 09:01

Angew is no longer proud of SO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!