Search code examples
c++cocos2d-xlinker-errorsstd-pairrapidjson

rapidjson::Document in std::pair


I am getting this error:

Undefined symbols for architecture i386:
rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> >::GenericValue(rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> > const&)

This error jumps at me when I try to return the rapidjson::Document in a pair:

typedef std::pair<rapidjson::Document, std::string> ProcessedResponseResult;

ProcessedResponseResult ProcessResponse(HttpResponse* response)
{
    rapidjson::Document jsonDoc;
    ...
    return ProcessedResponseResult(jsonDoc, std::string());
}

If it helps, rapidjson is a header only library.

Why can't I return the pair?


Solution

  • When you construct a ProcessedResponseResult, it will call the copy constructor of rapidjson::Document, but in the file rapidjason/document.h, to prevent from copying the rapidjson::Document object, it declares a private copy constructor, and doesn't implement it, so this causes the linker error.

    //! Copy constructor is not permitted.
    private:
        GenericValue(const GenericValue& rhs);
    

    If your reason for using std::pair is just to return 2 values from the function, I'd recommend passing the jsonDoc by reference.