全站文章索引 📚📚📚

展開全部 | 收合全部

全站文章索引 📚📚📚

展開全部 | 收合全部

【C++】問題解決:error: invalid initialization of reference of type ‘rapidjson::Document& {aka rapidjson::GenericDocument >&}’ from expression of type ‘rapidjson::GenericValue >’

問題描述

碰到以下開頭的內容,問題的解決方式

error: invalid initialization of reference of type ‘rapidjson::Document& {aka rapidjson::GenericDocument<rapidjson::UTF8<> >&}’ from expression of type ‘rapidjson::GenericValue<rapidjson::UTF8<> >’

問題來源

我原先想進行以下的操作

// Object (python dict)
cout << "[Reading json content] document[key6] = " << endl;
cout << "[Reading json content] Get type of document[key6] = " << kTypeNames[document["key6"].GetType()] << endl; // kTypeNames[itr->value.GetType()]);
Value &value6 = document["key6"]; 
Document d2; 
d2 = value6; // error: invalid initialization of reference of type ‘rapidjson::Document& {aka rapidjson::GenericDocument<rapidjson::UTF8<> >&}’ from expression of type ‘rapidjson::GenericValue<rapidjson::UTF8<> >’

出問題的段落在於 「d2 = value6」這個部分,
因為 value6 的格式為 Value,
而 d2 的格式為 Document,
所以當我們想進行等於的操作時,因為格式不同產生的問題。

說明

我們先解讀一下這個問題的內容敘述是什麼意思,
基本上我們可以抓到兩個關鍵字

  • rapidjson::GenericDocument
  • rapidjson::GenericValue

我們可以先看 rapidjson 官方的定義:

// Each JSON value is stored in a type called Value. 
// A Document, representing the DOM, contains the root Value of the DOM tree. 
// All public types and functions of RapidJSON are defined in the rapidjson namespace.

Document 全名為 DOM 格式 (Document Object Model, DOM),
包含著 DOM tree 裡面所有的 Value。
換句話說我們可以說,Document 可以用來作為儲存 Value 的文件格式。

我們理解上述內容後,我們就可以來看上面的 error 內容:
就是我們嘗試想把 Value 格式的內容引入至 Document,
因為格式沒有對應導致的問題。

解決方法

因為當我們想把 GenericDocument -> GenericValue 時,
可以進行以下的操作「tmpJsonObject.CopyFrom(json_content, json_content.GetAllocator()); 」。

// prepare json
Document d = make_json();
Value v(kObjectType);
v.CopyFrom(d, d.GetAllocator()); 

Reference

⭐C++ 基礎用法 相關文章整理⭐:
1.【C++】C++ compile 程式碼 使用 c++ 11 與使用相關的 package
2.【C++】C/C++ 顯示資料的類別 (type) sample code (內含範例程式碼) print C data type, cout C++ data type, get variable type in c++
3.【C++】C++ 複製 2D array的方法 copy 2d array memcpy sample code (內含範例程式碼)
⭐Modern C++ ⭐:
⭐C++ 字串處理相關文章整理⭐:
1.【C++】字串 char string stringstream 相關用法總整理 (內含範例程式碼) 與利用 sprinf, snprinf assign 值的方法
2.【C++】字串 char string stringstream 「轉換」用法總整理 (內含範例程式碼)
3.【C】printf, fprintf, sprintf, snprintf 相關用法總整理 (內含範例程式碼)
4.【C++】C++ String 用法 連接兩個 String c++ string concat
⭐C++ 系統偵測相關文章整理⭐:
1.【C++】C++ 利用 dirent.h 計算資料夾的檔案數量 count files sample code (內含範例程式碼)
2.【C++】C++ inotify sample code 偵測指定路徑底下的文件變化 (內有範例程式碼)
⭐C++ OpenCV 相關文章整理⭐:
1.【OpenCV】c++ OpenCV – 在 ubuntu 上第一次執行 OpenCV 程式 sample code (內含範例程式碼)
2.【OpenCV】c++ OpenCV - 在圖片上寫上文字 cv::putText sample code (內含範例程式碼)
3.【OpenCV】c++ OpenCV - cv::Rect 矩形用法與相關功能函數 sample code (內含範例程式碼)
4.【OpenCV】c++ OpenCV - OpenCV 中的純量 定義顏色 cv::Scalar(255,255,255) color sample code (內含範例程式碼)
5.【OpenCV】用 C++ 計算 iou 的方法 與網路算法常見錯誤(內附範例程式碼) sample code
⭐C++ Makefile 相關文章整理⭐:
1.【C++ Makefile】- 1 / 嘗試撰寫自己的第一份 Makefile
2.【C++ Makefile】- 2 / 新增自己的變數
3.【C++ Makefile】- 3 / Makefile 常用變數 -「$@」, 「$^」
4.【C++ Makefile】- 4 / Makefile 常用 fake target -「.PHONY」
5.【C++ Makefile】- 5 / Makefile 內建變數 -「$@」, 「$^」, 「$<」, 「$* 」,「$? 」
⭐【喜歡我的文章嗎? 歡迎幫我按讚~ 讓基金會請創作者喝一杯咖啡!
如果喜歡我的文章,請幫我在下方【按五下Like】 (Google, Facebook 免註冊),會由 「LikeCoin」 贊助作者鼓勵繼續創作,讀者們「只需幫忙按讚,完全不用出錢」哦!

likecoin-steps
Howard Weng
Howard Weng

我是 Howard Weng,很多人叫我嗡嗡。這個網站放了我的各種筆記。希望這些筆記也能順便幫助到有需要的人們!如果文章有幫助到你的話,歡迎幫我點讚哦!
另外,因為定位是「個人的隨手筆記」,有些文章內容「⚠️可能我理解有誤⚠️」或「🥱只寫到一半😴」,如果有發現這樣的情況,歡迎在該文章的最下面留言提醒我!我會儘快修正或補上!感謝大家的建議與幫忙,讓網站能變得更好🙏

文章: 817

★留個言吧!內容有誤或想要補充也歡迎與我討論!