-2
string toString() {

    std::stringstream punkte;
    std::stringstream name;
    std::cout << name << "hat" << punkte << "Punkte" << '\n'
    return 0;

  }

At this line of code. I'm receiving the error C++ << no operator found I can't figure out what my mistake is. I have read and tried different solutions. But nothing works. Can somebody please help?

 std::cout << name << "hat" << punkte << "Punkte" << '\n';

I also included this in my code:

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream, std::stringbuf
#include <fstream>
emaem1116
  • 23
  • 1

2 Answers2

0

There is no overload of operator<<() that will format a std::stringstream to a std::ostream. There error does not lie.

Stephen M. Webb
  • 1,705
  • 11
  • 18
0

You are trying to call operator "<<" with a stringstream parameter. In other words:

std::cout << name;

Is equivalent to:

std::cout.operator<<(name);

And that operator<<(const std::stringstream&) function doesn't exists.

I think that what you want to do is assign each stringstream their values and then print both, isn't?

string toString() 
{
    std::stringstream punkte;
    std::stringstream name;
    name << "hat";
    punkte << "Punkte";
    std::cout << name.str() << punkte.str() << std::endl;
    return name.str();
}

Be careful with your return value, and remember that a std::stringstream is not a std::string. If you want to retrieve the std:string in the stream, you must call the str() method.

LuisGP
  • 431
  • 3
  • 13
  • Thanks, but it's not exactly what I'm trying to do. name and punkte .. that's name and points in english and they are already initialized. I'm trying to become this Output "Gamer hat Points". – emaem1116 Jun 13 '18 at 20:18
  • The don't declare them again inside the function body, they will hide your attributes. To print out that string try: `std::cout << "Gamer " << name.str() << " hat " << punkte.str() << " Points." << std::endl;` – LuisGP Jun 13 '18 at 20:21
  • Hi @emaem116 if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – LuisGP Jun 13 '18 at 20:32