2014-07-11 34 views
2

我嘗試通過rapidjson :: Document對象作爲函數的參數:rapidjson對象作爲函數的參數原因編譯器錯誤

std::string json_to_string(rapidjson::Document jmsg) 
{ 
    // Convert JSON document to string 
    rapidjson::StringBuffer buffer; 
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); 
    jmsg.Accept(writer); 
    std::string str = buffer.GetString(); 
    return str; 
} 

如果我做了功能上面只是,我當我編譯的代碼得到這個錯誤:

在函數'rapidjson :: GenericDocument,rapidjson :: MemoryPoolAllocator> :: GenericDocument(rapidjson :: GenericDocument,rapidjson :: MemoryPoolAllocator>常量&)':

../../rapidjson/document。 h:691:對`rapidjson ::的未定義引用在minilang中,rapidjson :: MemoryPoolAllocator> ::在minilang中(rapidjson ::在minilang中,rapidjson :: MemoryPoolAllocator>常量&)」 collect2:錯誤:LD返回1種退出狀態,如果我更改從參數類型

錯誤消失「 rapidjson :: Document jmsg「改爲」rapidjson :: Document & jmsg「。使用引用是好的,但是,如果我沒有將它定義爲引用類型,我仍然想知道代碼有什麼問題。

回答

5

您無法將Document作爲值傳遞,您必須通過引用或指針傳遞它。 這是因爲Document不可複製。

我在您的情況建議此函數聲明:

std::string json_to_string(const rapidjson::Document& jmsg) 
+1

很高興見到你的左右。 ) – ArtemGr

+0

這工作就像一個魅力!但是如何指針不起作用?我試着將'rapidjson :: Document * doc_ptr'傳遞給我的函數,並且一直抱怨'Document'不可訪問。 – benjaminz