I tried to print the value that exists in the ostream variable by convert to string, and then to print the string instead the ostream argument.
But it's not working.
This is my code:
#include <iostream>
#include <cassert>
#include <cstring>
#include <sstream>
using std::ostream;
typedef std::basic_stringstream<char> stringstream;
class X {
public:
int y;
};
std::ostream& operator<<(std::ostream& os, const X x) {
return os << x.y;
}
int main() {
X x;
x.y = 5;
stringstream ss;
ostream output(nullptr);
output << x;
ss << output.rdbuf();
std::string myString = ss.str();
std::cout << x << std::endl; // 5
std::cout << "myString.c_str() :" << std::endl;
std::cout << myString.c_str() << std::endl; // nothing.
}
How can I fix this problem so that I will get at myString.c_str() the appropriate output?
Aucun commentaire:
Enregistrer un commentaire