【C++】問題解決:'error: no matching function for call to 'rapidjson::GenericValue<rapidjson::UTF8<> >::GenericValue(std::__cxx11::basic_string<char>&)' GenericValue v(value);

問題描述

C++ 使用 rapidjson 導致出現以下 error 的解決辦法。

/opt/work/lib/rapidjson/document.h:1065:29: error: no matching function for call to 'rapidjson::GenericValue<rapidjson::UTF8<> >::GenericValue(std::__cxx11::basic_string<char>&)'
         GenericValue v(value);

解法

此為資料型態錯誤問題,
有可能是例如 int 使用 string 的方式寫入(讀取)。

以我自己的經驗而言,
我嘗試寫 string 進入 rapidjson 當中,
但是我應該要先從 string 轉成 c_string,

也就是說

string teststr = "test";
result.AddMember("testkey",  Value().SetString(teststr.c_str(), allocator), allocator);

注意 SetString(teststr.c_str(), allocator) 的部份,
必須要將 string 轉換成 c_string

我之前沒有做這個轉換導致了這個 error。

Licensed under CC BY-NC-SA 4.0