Member Avatar for embooglement

I'm developing a game engine, and set up a function that uses string streams to convert various values into std::string's. To use this to string function on class obviously takes an overloaded insertion operator, which I have done for a transformation matrix class here:

typedef std::stringstream StreamString;

StringStream& operator<<(StringStream& stream, const TransMatrix& matrix)
{
	for (int y = 0; y < 4; y++)
	{
		stream << "{ ";
	
		for (int x = 0; x < 4; x++)
		{
			stream << matrix(x, y) << ((x < 3) ? ", " : " }\n");
		};
	};

	return stream;
};

I'm using Visual Studio C++ 2010 (which is probably my first mistake). The error I'm getting is " stream << "{ "; ". The compiler says that there are a bunch multiple overloads that fit that definition, including

std::basic_ostream<_Elem,_Traits> std::basic_ostream<_Elem,_Traits>::operator<<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))

std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator<<(std::_Bool)

std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator<<(const void *)

and more or less every definition of the << operator for string stream. However, it doesn't include one for const char*, which is obviously the one I want. What's more is this code worked exactly as expected for a while, but suddenly stopped. I don't remember changing anything that would have affected this, but maybe I'm wrong.

Anyways, anybody have any advice?

Recommended Answers

All 3 Replies

did you include <sstring> ?

>>? ", " : " }\n");
Split that out into an if statement and see what the compiler says about it.

stream << matrix(x, y);
if( x < 3 )
   stream << ", ";
else
   stream << " )\n";

I hope this is a typo :

typedef std::stringstream StreamString;
StringStream& operator<<(StringStream& stream, const TransMatrix& matrix)

I'm not sure whats the problem. Are you sure the errors occurs at line 7?

Member Avatar for embooglement

Oh, yeah, that was a typo. I didn't actually copy and paste the typedef, I just included that to make it clear that I was using the std::stringstream without having to change the rest of the code, which I did copy and paste.

And in regards to the problem, at the time of posting, it was occurring at all points where I tried to use a string stream and a const char*, which was fairly frequent. Now however the problem appears to have gone away in the TransMatrix class, though it still persists in other classes, despite the operator declaration being exactly the same. Also, the only changes I made to TransMatrix was the one suggested by Ancient Dragon. However the line that was causing the error was most definitely line 7. Like I said, this code worked a week ago, and seemingly stopped working without change, and I get the feeling that it's working in TransMatrix is due to the same magic voodoo Visual Studio uses to compile code, and will soon enough be broken again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.