Pregunta

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!

¿Fue útil?

Solución

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

Otros consejos

In order to use stringstream you need to #include <sstream>. Also stringstream is defined in the std namespace, but from your code it seems you are using this namespace already. The other parts of the code seems correct to me.

You are trapped by a forward declaration of stringstream - just #include <sstream>

Also use std::stringstream, std::string and return ss.str() (scrape that string output)

Further: Putting a using namespace::std in a header (global scope) is no good. See: Using std Namespace

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top