首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >针对QList<Class*>的QDatastream operator>>

针对QList<Class*>的QDatastream operator>>
EN

Stack Overflow用户
提问于 2011-09-12 06:55:34
回答 2查看 5.8K关注 0票数 4

因此,我为定制类重载了QDatastream的>>和<<操作符。我制作了两个版本;一个用于基本对象,另一个用于对象的指针。到目前为止,所有版本的运算符都可以正常工作,但有一个例外。指针读取操作符会读取正确的数据,但该数据未正确保存在QList<*>中。

下面是一些示例代码。

代码语言:javascript
复制
QDataStream & operator <<(QDataStream &dataStream, const Faction &rhs)
{
    return rhs.write(dataStream);
}

QDataStream & operator >>(QDataStream &dataStream, Faction &rhs)
{
    return rhs.read(dataStream);
}

QDataStream & operator <<(QDataStream &dataStream, const Faction *rhs)
{
    return rhs->write(dataStream);
}

QDataStream & operator >>(QDataStream &dataStream, Faction *rhs)
{
    rhs = new Faction();
    return rhs->read(dataStream);
}

QDataStream & Faction::read(QDataStream &dataStream)
{
    QString tag;
    dataStream >> tag;

    QString classTag = QString(typeid(this).name());
    getTypeName(&classTag);
    if (tag == classTag + "Start")
    {
        while (tag != classTag + "End")
        {
            if (tag == "name")
            {
                dataStream >> name;                  // The name of the faction.
            }
            else if (tag == "buildings")
            {
                dataStream >> buildings;             // The buildings of the Faction.
            }
            else if (tag == "units")
            {
                dataStream >> units;                 // The units of the Faction.
            }
            else if (tag == "upgrades")
            {
                dataStream >> upgrades;              // The upgrades of the Faction.
            }
            else if (tag == "startBuildings")
            {
                dataStream >> startBuildings;    // The list of buildings when starting a game.
            }
            else if (tag == "startUnits")
            {
                dataStream >> startUnits;        // The list of units when starting a game.
            }
            else if (tag == "startUpgrades")
            {
                dataStream >> startUpgrades;     // The list of upgrades when starting a game.
            }

            // Reading the next tag.
            dataStream >> tag;
        }
    }
    return dataStream;
}

QDataStream & Faction::write(QDataStream &dataStream) const
{
    QString classTag = QString(typeid(this).name());
    getTypeName(&classTag);
    dataStream << QString(classTag + "Start");

    dataStream << QString("name");
    dataStream << name;                           // The name of the faction.
    dataStream << QString("buildings");
    dataStream << buildings;             // The buildings of the Faction.
    dataStream << QString("units");
    dataStream << units;                 // The units of the Faction.
    dataStream << QString("upgrades");
    dataStream << upgrades;              // The upgrades of the Faction.
    dataStream << QString("startBuildings");
    dataStream << startBuildings;    // The list of buildings when starting a game.
    dataStream << QString("startUnits");
    dataStream << startUnits;        // The list of units when starting a game.
    dataStream << QString("startUpgrades");
    dataStream << startUpgrades;     // The list of upgrades when starting a game.

    dataStream << QString(classTag + "End");

    return dataStream;
}

Faction.h

代码语言:javascript
复制
    #ifndef FACTION_H
    #define FACTION_H

    #include <JECUtils.h>

    #include <Unit.h>
    #include <UnitBase.h>

    class Faction
    {
    public:
        explicit Faction();
        explicit Faction(const QString& name);
        Faction(const Faction& faction);

        Faction& operator=(const Faction& rhs);
        bool operator==(const Faction& rhs) const;
        bool operator!=(const Faction& rhs) const;

        friend QDataStream &operator<<(QDataStream &dataStream, const Faction& rhs);
        friend QDataStream &operator>>(QDataStream &dataStream, Faction& rhs);
        friend QDataStream &operator<<(QDataStream &dataStream, const Faction* rhs);
        friend QDataStream &operator>>(QDataStream &dataStream, Faction* rhs);

        void addBuilding(UnitBase* building);
        void addUnit(UnitBase* unit);
        void addUpgrade(UnitBase* upgrade);

        const QString& getName() const;
        const UnitBase* getBuilding(const int& index) const;
        const QList<UnitBase*>& getBuildings() const;
        const UnitBase* getUnit(const int& index) const;
        const QList<UnitBase*>& getUnits() const;
        const UnitBase* getUpgrade(const int& index) const;
        const QList<UnitBase*>& getUpgrades() const;
        const QList<QList<Unit*> >* getStartUnits() const;
        const QList<QList<Unit*> >* getStartBuildings() const;
        const QList<QList<Unit*> >* getStartUpgrades() const;

        void initialize(const QStringList& globalActions);

        void removeAllBuilding();
        void removeAllUnit();
        void removeAllUpgrade();
        void removeBuilding(const int& index);
        void removeUnit(const int& index);
        void removeUpgrade(const int& index);

        void setStartUp(const QStringList& names, const QList<int>& quantities);

    protected:
        QDataStream& read(QDataStream &dataStream);
        QDataStream& write(QDataStream &dataStream) const;

    private:
        QString name;                           // The name of the faction.
        QList<UnitBase*> buildings;             // The buildings of the Faction.
        QList<UnitBase*> units;                 // The units of the Faction.
        QList<UnitBase*> upgrades;              // The upgrades of the Faction.
        QList<QList<Unit*> > startBuildings;    // The list of buildings when starting a game.
    QList<QList<Unit*> > startUnits;        // The list of units when starting a game.
    QList<QList<Unit*> > startUpgrades;     // The list of upgrades when starting a game.
};

#endif // FACTION_H

所以在这个特定的例子中,我有一个QList。当代码

代码语言:javascript
复制
dataStream >> factions

是运行的,派系*的整个QList都应该被读入。然而,我得到了垃圾。

代码语言:javascript
复制
QDataStream & operator >>(QDataStream &dataStream, Faction *rhs)
{
    rhs = new Faction();
    return rhs->read(dataStream); <---- rhs will return good data.
}

rhs包含从文件中读取的正确数据。但是,在读取完整个QList之后,QList中就会有一些无用的值。

有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-12 15:18:09

运算符>>期望引用作为其第二个参数,您也可以引用指针:

代码语言:javascript
复制
QDataStream & operator >>(QDataStream &dataStream, Faction *&rhs)
{
    rhs = new Faction();
    return rhs->read(dataStream);
}
票数 6
EN

Stack Overflow用户

发布于 2011-09-12 07:28:07

因为您希望更改rhs指向的内容,所以应该将指向指针的指针作为参数传递给运算符。

同样,这样做,指针的更改将在函数返回时反映出来。

代码语言:javascript
复制
QDataStream & operator >>(QDataStream &dataStream, Faction **rhs)
{
    *rhs = new Faction();
    return (*rhs)->read(dataStream); <---- rhs will return good data.
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7381843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档