c++ 将rapidjson::Object传递给方法

e0bqpujr  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(151)

长话短说:如何将rapidjson::Object传递给方法?
详细说明:我有以下代码:

void CMyClass::SomeMethod()
{
    rapidjson::Document doc;
    doc.Parse(data.c_str(), data.size());

    std::string output{};
    for (const auto& obj : doc.GetObj())
    {
        if ("My_data" == obj.name)
        {
            ReadMyData(obj, output);
            continue;
        }
    }
}

void CMyClass::ReadMyData(???, std::string& output)
{
    //
}

字符串
我试过:

// header
    template<bool Flag, typename T>
    void ReadMyData(const rapidjson::GenericObject<Flag, T>& obj, std::string& output);

// implementation
template<bool Flag, typename T>
void CMyClass::ReadMyData(const rapidjson::GenericObject<Flag, T>& obj, SWeather& output)
{

}


但我得到了

.cpp(109): error C2672: 'CMyClass::ReadMyData': no matching overloaded function found
.cpp(109): error C2784: 'void CMyClass::ReadMyData(const rapidjson::GenericObject<Const,ValueT> &,std::string &)': could not deduce template argument for 'const rapidjson::GenericObject<Const,ValueT> &' from 'const rapidjson::GenericMember<Encoding,Allocator>'
1>        with
1>        [
1>            Encoding=rapidjson::UTF8<char>,
1>            Allocator=rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>
1>        ]


更多信息:GetObj(),是:

Object GetObj() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); }


Object

template <typename Encoding, typename Allocator = RAPIDJSON_DEFAULT_ALLOCATOR >
class GenericValue {
public:
    //! Name-value pair in an object.
    typedef GenericMember<Encoding, Allocator> Member;
    typedef Encoding EncodingType;                  //!< Encoding type from template parameter.
    typedef Allocator AllocatorType;                //!< Allocator type from template parameter.
    typedef typename Encoding::Ch Ch;               //!< Character type derived from Encoding.
    typedef GenericStringRef<Ch> StringRefType;     //!< Reference to a constant string
    typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator;  //!< Member iterator for iterating in object.
    typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator;  //!< Constant member iterator for iterating in object.
    typedef GenericValue* ValueIterator;            //!< Value iterator for iterating in array.
    typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array.
    typedef GenericValue<Encoding, Allocator> ValueType;    //!< Value type of itself.
    typedef GenericArray<false, ValueType> Array;
    typedef GenericArray<true, ValueType> ConstArray;
    typedef GenericObject<false, ValueType> Object;
    typedef GenericObject<true, ValueType> ConstObject;


rapidjson命名空间中。

vwoqyblh

vwoqyblh1#

迭代一个对象返回一个GenericMember而不是GenericObject,你需要:

for (const auto& member : doc.GetObj())
{
    if ("My_data" == member.name && member.value.IsObject())
    {
        ReadMyData(member.value.GetObject(), output);
        continue;
    }
}

字符串
除非您需要能够科普其他编码,否则可以将ReadMyData更改为非模板,而仅将rapidjson::Value::Object作为obj参数

相关问题