Fixing error C2338: Test writer must define specialization of ToString<> error

SneWs's picture

Visual Studio 2012 ships with a new Native C++ test framework. When writing tests and hitting a error message like this:
error C2338: Test writer must define specialization of ToString<const Q& q> for your class class
std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >
__cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString<SomeType>(const SomeType &).

You will have to impl. support for the missing type, that's easy to fix, just create a new file in your test project and impl. the function. Here is a quick example defining support for the custom type SomeType:

namespace Microsoft
{ 
    namespace VisualStudio
    { 
        namespace CppUnitTestFramework
        {
            template<> static std::wstring ToString<SomeType>(const SomeType& t) { RETURN_WIDE_STRING(t); }
        }
    }
}

Enjoy Native Unit testing!

3 comments

by Andrey-FreneEnum on Wed, 02/06/2013 - 10:34

Спасибо за чудо))

by James on Fri, 04/26/2013 - 18:19

Thanks for your post, but I get "Error: ToString is not a template." Am I putting this in the wrong place?

SneWs's picture

by SneWs on Fri, 05/03/2013 - 04:11

Are you working with raw pointers? If so you also need to have have a template specialization for the pointer case. It's hard to know without having a extracted example of what you are trying to do.

To define an overload for a pointer to SomeType you would specify something like:

template static std::wstring ToString(SomeType* t) { RETURN_WIDE_STRING(*t); }

Post new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
© 2008 - 2012 Marcus Grenängen. Drupal theme by Kiwi Themes.