I'm trying to write a to string function that would use std::stringstream to convert the the instance data into a string, but haven't been able to get operator overloading to work with it. As soon as I define a new << operator, I get a bunch of ambiguity errors saying that there are multiple overload for the operator that could fit. I'm using a trial version Visual Studio 2010 Proffesional, and had the same problem using Visual C++ 2010 Express.
Here's the code, and the line that's causing the error is "stream << someobject.num;"
#include <sstream>
#include <iostream>
class A
{
public:
int num;
A(int x) : num(x) {};
friend std::stringstream& operator<<(std::stringstream& stream, const A& someobject);
};
std::stringstream& operator<<(std::stringstream& stream, const A& someobject)
{
stream << someobject.num;
return stream;
};
int main(int, char**)
{
A a1(7);
std::stringstream stream1;
stream1 << "Hello world! " << a1;
std::cout << stream1.str() << std::endl;
system("pause");
return 0;
};