CString ToString() 模板重载 for CppUnitTestFramework.
CString ToString() template overload for CppUnitTestFramework
如何为 CStrings编写C++ VS2012 CppUnitTestFramework ToString() 模板重载?这必须是可能的。
编译器说我必须,因为它给出了编译错误:
error C2338: Test writer must define specialization of ToString<const Q& q> for your class class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString<class ATL::CStringT<unsigned short,class StrTraitMFC<unsigned short,class ATL::ChTraitsCRT<unsigned short> > >>(const class ATL::CStringT<unsigned short,class StrTraitMFC<unsigned short,class ATL::ChTraitsCRT<unsigned short> > > &).
对于其他数据类型,我在单元测试代码中添加了与此类似的内容:
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Microsoft { namespace VisualStudio { namespace CppUnitTestFramework {
template<> static std::wstring ToString<eNumy>(const eNumy & e) { return ltots(static_cast<long>(e)); }
我尝试添加:
template<> static std::wstring ToString<CString>(const CString & e) { return e.GetBuffer(); }
但这无法与 const 问题一起编译:
error C2663: 'ATL::CSimpleStringT<BaseType>::GetBuffer' : 2 overloads have no legal conversion for 'this' pointer
有人有什么想法吗?
我想运行这个测试:
CString csTina;
CString csGeoff;
Assert::AreNotEqual(csTina, csGeoff);
是的,你有它:CString
的函数GetBuffer
未标记为const
函数,因此您无法在const CString&
上调用此函数。
多种选择:
- 从重载中删除
const
。 - 在没有
GetBuffer
的情况下实现逻辑。 - 执行
const_cast
以获取它。(虽然不推荐)。 - 只需将
CString
转换为std::string
(或std::wstring
)并使用现有功能即可。
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 继承函数的重载解析
- 如何在C++中从两个单独的for循环中添加两个数组
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 在Linux for Windows上编译C++代码时出错
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- 在命名空间内的类中使用带有运算符重载的字符串流时"no match for ‘operator>>’"
- CString ToString() 模板重载 for CppUnitTestFramework.
- 运算符重载错误"no match for operator error"
- 如果我没有重载,为什么我会"no match for ‘operator=’"自定义类?
- 重载<运算符在另一个类中使用时"no matching function for call"错误
- 为什么 range-for 找不到 std::istream_iterator 的开始和结束重载?
- ostream 重载与 for 循环,无返回值
- 重载运算符 << for std::tuple - 可能的干扰?
- C++Visual Studio-运算符重载中的Empty For循环导致调试错误
- "ambiguous overload for 'operator<<'" *没有*包罗万象的重载