Microsoft Visual Studio 单元测试的 C2338 编译错误
C2338 compile error for a Microsoft Visual Studio unit test
我在 Visual Studio 2013 年尝试编译单元测试时收到以下错误:
Error 1 error C2338: Test writer must define specialization of ToString<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<struct HINSTANCE__>(struct HINSTANCE__ *).
您可以通过如下测试方法重现错误:
const std::wstring moduleName = L"kernel32.dll";
const HMODULE expected = GetModuleHandle(moduleName.c_str());
Microsoft::VisualStudio::CppUnitTestFramework::Assert::AreEqual(expected, expected);
有谁知道我需要如何着手编写 ToString
的这种专业化?
我通过将以下代码添加到我的单元测试 class 文件中设法解决了这个问题:
/* ToString specialisation */
namespace Microsoft
{
namespace VisualStudio
{
namespace CppUnitTestFramework
{
template<> static std::wstring ToString<struct HINSTANCE__>
(struct HINSTANCE__ * t)
{
RETURN_WIDE_STRING(t);
}
}
}
}
我根据 CppUnitTestAssert.h 的内容(这是编译错误发生的地方 - 双击编译错误将为您打开此文件)。
在文件顶部附近(如果您双击上述编译错误,则只有几行下方)您可以看到一组 ToString
模板。我复制了其中一行并将其粘贴到我的测试 class 文件中(包含在与原始模板相同的名称空间中)。
然后我简单地修改了模板以匹配编译错误(特别是 <struct HINSTANCE__>(struct HINSTANCE__ * t)
)。
对于我的场景,使用 RETURN_WIDE_STRING(t)
足以显示我的 AreSame
断言中的不匹配。根据使用的类型,您可以进一步提取一些其他有意义的文本。
我在比较 class 个对象时遇到了同样的问题。
对我来说,我可以通过简单地写
来解决它
Assert::IsTrue(bitmap1 == bitmap2);
而不是
Assert::AreEqual(bitmap1, bitmap2);
截至2021年,ClassSkeleton提供的答案对我不起作用,但我在其基础上进行了一些修改并编译了以下内容。基本上,错误消息后面的几行提供了一些示例。
template<>
inline std::wstring __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString<MyClass>(const MyClass& t)
{
// replace with your own, here is just my example
// RETURN_WIDE_STRING(t.ToString().c_str());
}
将 MyClass
替换为您的 class。
我在 Visual Studio 2013 年尝试编译单元测试时收到以下错误:
Error 1 error C2338: Test writer must define specialization of ToString<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<struct HINSTANCE__>(struct HINSTANCE__ *).
您可以通过如下测试方法重现错误:
const std::wstring moduleName = L"kernel32.dll";
const HMODULE expected = GetModuleHandle(moduleName.c_str());
Microsoft::VisualStudio::CppUnitTestFramework::Assert::AreEqual(expected, expected);
有谁知道我需要如何着手编写 ToString
的这种专业化?
我通过将以下代码添加到我的单元测试 class 文件中设法解决了这个问题:
/* ToString specialisation */
namespace Microsoft
{
namespace VisualStudio
{
namespace CppUnitTestFramework
{
template<> static std::wstring ToString<struct HINSTANCE__>
(struct HINSTANCE__ * t)
{
RETURN_WIDE_STRING(t);
}
}
}
}
我根据 CppUnitTestAssert.h 的内容(这是编译错误发生的地方 - 双击编译错误将为您打开此文件)。
在文件顶部附近(如果您双击上述编译错误,则只有几行下方)您可以看到一组 ToString
模板。我复制了其中一行并将其粘贴到我的测试 class 文件中(包含在与原始模板相同的名称空间中)。
然后我简单地修改了模板以匹配编译错误(特别是 <struct HINSTANCE__>(struct HINSTANCE__ * t)
)。
对于我的场景,使用 RETURN_WIDE_STRING(t)
足以显示我的 AreSame
断言中的不匹配。根据使用的类型,您可以进一步提取一些其他有意义的文本。
我在比较 class 个对象时遇到了同样的问题。 对我来说,我可以通过简单地写
来解决它Assert::IsTrue(bitmap1 == bitmap2);
而不是
Assert::AreEqual(bitmap1, bitmap2);
截至2021年,ClassSkeleton提供的答案对我不起作用,但我在其基础上进行了一些修改并编译了以下内容。基本上,错误消息后面的几行提供了一些示例。
template<>
inline std::wstring __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString<MyClass>(const MyClass& t)
{
// replace with your own, here is just my example
// RETURN_WIDE_STRING(t.ToString().c_str());
}
将 MyClass
替换为您的 class。