0

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

Joe White
  • 94,807
  • 60
  • 220
  • 330
Martin
  • 73
  • 5
  • I'm wrong or is TestStruct which should implement toStting? – Marco Acierno Jul 06 '14 at 11:55
  • That's what I wondered about. However according to all the examples I've seen online they are implements as static methods that belong to the Microsoft::VisualStudio::CppUnitTestFramework namespace. Which kind of makes sense as you may be testing a class in a library and can't edit it to add the ToString method. – Martin Jul 06 '14 at 12:03
  • The error message indicates that `TestStruct` resides in a namespace `CoreUnitTests`, but your sample code does not reflect this. Are you perhaps accidentally defining your `ToString` function in the namespace `CoreUnitTests::Microsoft::VisualStudio::CppUnitTestFramework`? – Lilshieste Jul 06 '14 at 17:22

0 Answers0