2016-09-22 28 views
0

我有以下代碼,但它不能編譯。 我想不出一個理由,請高興。RapidJson kArrayType與字符串

rapidjson::Document jsonDoc; 
jsonDoc.SetObject(); 
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator(); 

rapidjson::Value messageArr(rapidjson::kArrayType); 

std::string test = std::string("TEST"); 
messageArr.PushBack(test.c_str(), allocator); 

給我下面的錯誤;

error: no matching function for call to ‘rapidjson::GenericValue >::PushBack(const char*, rapidjson::GenericDocument >::AllocatorType&)’
messageArr.PushBack(test.c_str(), allocator);

+0

做 - RapidJosn有不同類型的字符串值:分配(需要構造時的長度),簡單的'const char *'包裝(如果超出範圍將會被吹掉)和/或'short strings' * 15 ch ars或更少 - 或者類似的)。既然你想用分配器,我想你想要一個副本的StrValue - 答案顯示瞭如何。 –

回答

1

[編輯] - 解決方案:

std::string test = std::string("TEST"); 
    rapidjson::Value strVal; 
    strVal.SetString(test.c_str(), test.length(), allocator); 
    messageArr.PushBack(strVal, allocator); 

RapidJson tutorial - Create String

流利的風格:

messageArr.PushBack(
     rapidjson::Value{}.SetString(test.c_str(), test.length(), allocator), 
     allocator 
); 
+1

我試過了,但沒有運氣,然後給出了以下錯誤,從這裏得到 /usr/include/rapidjson/document.h:1342:29:錯誤:沒有匹配函數調用'rapidjson :: GenericValue > :: GenericValue(std :: __ cxx11 :: basic_string &)' GenericValue v(value); ^ – nilan

+0

你有沒有得到這個編譯?當我嘗試推回我的rapidjson :: Value時,我遇到了同樣的錯誤,就像他在談論copyFrom時在rapidjson教程中所做的一樣。 – Michele