1

I have the following problem when I try to call method in "TestSerialize" class during serialization process.

Here is my code:

class TestSerialize
{
public:
    std::string GetVal() { return Val + "abc"; }
    void SetVal(std::string tVal) { Val = tVal.substr(0, 2); }

protected:

    std::string Val;

    friend class boost::serialization::access;
    template<class Archive> void save(Archive & ar, const unsigned int version) const
    {
        using boost::serialization::make_nvp;
        std::string tVal = GetVal(); // Error here
        ar & make_nvp("SC", tVal);
    }

    template<class Archive> void load(Archive & ar, const unsigned int version)
    {
        using boost::serialization::make_nvp;
        std::string tVal;
        ar & make_nvp("SC", tVal);
        SetVal(tVal);
    }
    BOOST_SERIALIZATION_SPLIT_MEMBER();
};

int main()
{
    TestSerialize tS;

    std::ofstream ofs("test.xml");
    boost::archive::xml_oarchive oa(ofs, boost::archive::no_header);
    oa << BOOST_SERIALIZATION_NVP(tS);
    ofs.close();

    return 0;
}

The error that I encountered is: 'TestSerialize::GetVal' : cannot convert 'this' pointer from 'const TestSerialize' to 'TestSerialize &'

This error only happens on "save" but not "load"

I wonder why I get this error. I would like to know what Boost.Serialization do such that we have these two different behaviors. I use Boost Library 1.47.0

1 Answer 1

2

save is a const function and can only call other const functions. GetVal isn't. Change it:

std::string GetVal() const { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.