Qt 序列化自定义 class

Qt Serializion custom class

我的代码有什么问题!!!!我收到以下错误消息: 未解析的外部符号。 任何建议..以及从 QObject 继承的正确方法是什么让您尝试访问私有成员。

namespace BioQt {
     class Location : public QObject
    {
        Q_OBJECT
    public:
        explicit Location(QObject *parent );

    };
    QDataStream &operator<<(QDataStream &out, const Location &obj);
    QDataStream &operator>>(QDataStream &in, Location &obj);

    }


    Q_DECLARE_TYPEINFO(BioQt::Location, Q_PRIMITIVE_TYPE);

#endif

这是我的 cpp 文件

namespace BioQt {

Location::Location(QObject *parent)
    : QObject(parent)
{

}



QDataStream &operator<<(QDataStream &ds, const Location &obj) {
    for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
        if(obj.metaObject()->property(i).isStored(&obj)) {
            ds << obj.metaObject()->property(i).read(&obj);

        }
    }
    return ds;
}
QDataStream &operator>>(QDataStream &ds, Location &obj) {
    QVariant var;
    for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
        if(obj.metaObject()->property(i).isStored(&obj)) {
            ds >> var;
            obj.metaObject()->property(i).write(&obj, var);
        }
    }
    return ds;
}

} // namespace BioQt

我成功地使用 Qt 5.4.0 vs2010 x86 进行了编译,没有出现任何错误。回到使用 Qt 4.7 的日子里,我记得 sthg 类似,将主体移动到头文件并使用 inline 对我有用,我记得。