【发布时间】:2015-02-04 21:12:53
【问题描述】:
我正在尝试使用带有 Microsoft ATL CString 类型的 rapidjson 库,如下例所示。
#include "stdafx.h"
#include "rapidjson\document.h"
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
int main()
{
WDocument document;
CString hello = _T("Hello");
document.SetObject();
document.AddMember(_T("Hello"), hello, document.GetAllocator());
return 0;
}
编译器错误导致失败
'rapidjson::GenericValue::GenericValue(rapidjson::GenericValue &&)': 无法将参数 1 从 'CString' 转换为 'rapidjson::Type' rapidjson document.h 1020
这确实意味着需要在 CString 和 rapidjson 需要的格式之间进行转换。我知道 rapidjson 在内部使用 wchar_t 作为其函数的 UTF16 版本的编码,但是我不确定如何以 rapidjson 能够将字符串用作它使用 _T 宏定义的字符串。
我查看了有关在字符串类型here 之间进行转换的 msdn 资源,但这仅提供了一种方法来返回指向 wchar_t 数组的第一个成员的指针,然后 rapidjson 无法使用该指针。
【问题讨论】:
-
确保构建 Unicode 构建,否则使用
CStringW和L"Hello"(在 ANSI 构建中,CString存储chars,而不是wchar_ts)。您可能需要显式转换为const wchar_t*(CStringW提供了合适的转换运算符)。 -
感谢您提出这个建议,转换为 wchar_t* 的想法帮助我弄清楚该怎么做,我现在会给出答案。
标签: c++ visual-studio-2013 mfc atl rapidjson