简体   繁体   中英

error: invalid operands to binary expression

I was trying to compile the code from a third party and got the error:

error: invalid operands to binary expression

('boost::archive::binary_oarchive' and 'Tree *') oa << this;

I believe it is illegal to pass this to oa using << . But can anyone tell me how to start fixing it?

This is the source code:

void save(std::string path) {
    try {
        std::ofstream ofs(path.c_str());
        boost::archive::binary_oarchive oa(ofs);
        oa << this;
        ofs.flush();
        ofs.close();
        std::cout << "saved " << path << std::endl;
    } catch (boost::archive::archive_exception& ex) {
        std::cout << "Archive Exception during serializing:" << std::endl;
        std::cout << ex.what() << std::endl;
        std::cout << "it was tree: " << path << std::endl;
    }
}

[Edit: Actually, what @LightnessRacesinOrbit says makes more sense: you probably just need to dereference the this pointer]

It seems like operator << isn't defined for class Tree . It would need to conform to whatever format boost::archive::binary_oarchive is expecting.

A definition of operator << for Tree would be:

boost::archive::binary_oarchive& operator<<(boost::archive::binary_oarchive& os, const Tree& dt)
{
    // TODO: serialise 'tree' into 'os' 

    return os;
}

If you need to use private fields of the Tree class, make it a friend function by putting the following inside Tree 's declaration:

boost::archive::binary_oarchive& operator<<(boost::archive::binary_oarchive& os, const Tree& dt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM