Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rapidjson object as function argument cause compiler error

I try to pass rapidjson::Document object as function argument:

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;
}

If I do the function just as above, I got this error when I compile the code:

In function `rapidjson::GenericDocument, rapidjson::MemoryPoolAllocator >::GenericDocument(rapidjson::GenericDocument, rapidjson::MemoryPoolAllocator > const&)':

../../rapidjson/document.h:691: undefined reference to `rapidjson::GenericValue, rapidjson::MemoryPoolAllocator >::GenericValue(rapidjson::GenericValue, rapidjson::MemoryPoolAllocator > const&)' collect2: error: ld returned 1 exit status

The error disappears if I change the argument type from "rapidjson::Document jmsg" to "rapidjson::Document &jmsg". Use the reference is ok, however, I still want to know what's wrong with the code if I don't define it as a reference type.

like image 562
Max Li Avatar asked Aug 12 '26 02:08

Max Li


1 Answers

You cannot pass a Document as value, you must pass it by reference or pointer. This is because Document is not copy-able.

I suggest this function declaration in your situation:

std::string json_to_string(const rapidjson::Document& jmsg)
like image 133
Milo Yip Avatar answered Aug 19 '26 07:08

Milo Yip