错误:二进制表达式的操作数无效

error: invalid operands to binary expression

本文关键字:操作数 无效 表达式 二进制 错误      更新时间:2023-10-16

我试图从第三方编译代码,但得到了错误:

错误:二进制表达式的操作数无效

('boost::archive::binary_oachive'和'Tree*')oa<lt;这

我认为使用<<this传递给oa是非法的。但有人能告诉我如何开始修复它吗?

这是源代码:

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;
    }
}

[编辑:实际上,@LightnessRacesinOrbit所说的更有意义:你可能只需要取消引用this指针]

似乎没有为类Tree定义operator <<。它需要符合boost::archive::binary_oarchive所期望的任何格式。

operator <<对于Tree的定义是:

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

如果您需要使用Tree类的私有字段,请在Tree的声明中放入以下内容,使其成为友元函数:

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