错误C2338:测试编写者必须为您的班级定义ToString <const q&=“”q =“”>的特化

时间:2016-05-27 21:01:02

标签: unit-testing visual-studio-2013 c++-cli

我目前在Visual Studio 2013 C ++ / CLI本机测试中遇到以下错误。

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<class LTI::IPCommon::CPPBoxesBuffer>(const class LTI::IPCommon::CPPBoxesBuffer &).  c:\program files (x86)\microsoft visual studio 12.0\vc\unittest\include\cppunittestassert.h

我已经尝试了VS2012 : Error with unit test : Assert::AreEqual( object, object ) didn't work中建议的解决方案并写了一个专门化但是没有用。

这是BoxesBuffer.h中的类:

    class CPPBoxesBuffer{        
        private:
            unsigned char* _data;
            int _lines;

        public:
            CPPBoxesBuffer(unsigned char* data, int lines){ _data = data; _lines = lines; };

            // Methods for std::map. Class must be Assignable
            CPPBoxesBuffer(){ _data = nullptr; _lines = 0; };

            CPPBoxesBuffer(const CPPBoxesBuffer& other)
            {
                _lines = other._lines;
                _data = other._data;
            }

            CPPBoxesBuffer& operator=(const CPPBoxesBuffer& other)
            {
                _lines = other._lines;
                _data = other._data;
                return *this;
            }

            bool operator==(const CPPBoxesBuffer& other) const
            {
                return _lines == other._lines && _data == other._data;
            }


    };

以下是测试:

    TEST_METHOD(ShouldSetAndGetCppBoxesBufferData)
    {
        std::string key = "WhenPassingData";
        unsigned char t[5] = { 'a', 'b', 'c', 'd', 'e' };
        CPPBoxesBuffer data(t, 5);
        auto result = data;
        auto db = data == result; // This compiles.

        Assert::AreEqual(data, result);  // This fails with the error.
    }

这是我写的专业:

#include "CppUnitTest.h"
#include "BoxesBuffer.h"

namespace Microsoft
{
    namespace VisualStudio
    {
        namespace CppUnitTestFramework
        {
            template<> static std::wstring ToString<CPPBoxesBuffer>(const class CPPBoxesBuffer& t) { return L"CppBoxesBuffer"; }
        }
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的专业化是否在Assert::AreEqual行可以看到的地方声明了?我相信,如果它在一个不同的cpp文件而不是标题中的原型,那么在编译Assert::AreEqual时就找不到它。