3

我尝试将 rapidjson::Document 对象作为函数参数传递:

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 > const&)'中:

../../rapidjson/document.h:691: 未定义引用`rapidjson::GenericValue, rapidjson::MemoryPoolAllocator >::GenericValue(rapidjson::GenericValue, rapidjson::MemoryPoolAllocator > const&)' collect2: error: ld返回 1 个退出状态

如果我将参数类型从“rapidjson::Document jmsg”更改为“rapidjson::Document &jmsg”,错误就会消失。使用引用是可以的,但是,如果我不将其定义为引用类型,我仍然想知道代码有什么问题。

4

1 回答 1

6

您不能将 aDocument作为值传递,您必须通过引用或指针传递它。这是因为Document不可复制。

我建议在您的情况下使用此函数声明:

std::string json_to_string(const rapidjson::Document& jmsg)
于 2014-07-14T01:45:21.063 回答