I have a unit test defined in VS 2012 and it won't compile because of error C2338.
error C2338: Test writer must define specialization of ToString for your class class std::basic_string,class std::allocator > __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString(const struct CoreUnitTests::TestStruct &). c:\program files (x86)\microsoft visual studio 11.0\vc\unittest\include\cppunittestassert.h
This occurs when does an Assert::AreEqual test. I need to define a ToString method for the type.
I followed the guidance given in cppunittestassert.h and also found the solution on the internet which I've put in. However the error is still occurring.
Here's an example of the code I'm using:
struct TestStruct
{
public:
float f;
int i;
bool operator == (const TestStruct& rhs) const
{
return (f == rhs.f) && (i == rhs.i);
}
};
namespace Microsoft
{
namespace VisualStudio
{
namespace CppUnitTestFramework
{
template<>
static std::wstring ToString<TestStruct>(const TestStruct& t)
{
std::wstringstream stream;
stream << "TestStruct";
return stream.str();
}
}
}
}
I'm obviously still doing something wrong. Anyone have any ideas. I have already tried adding in a TestStruct* version and that doesn't help.
I suspect it may have something to do with namespaces but I'm not sure how to fix this problem.
Thanks