gpt4 book ai didi

qt - QDatastream operator>> 用于 QList

转载 作者:行者123 更新时间:2023-12-05 00:03:00 42 4
gpt4 key购买 nike

所以我为自定义类重载了 QDatastream 的 >> 和 << 运算符。我制作了 2 个版本;一个用于基础对象,一个用于对象的指针。到目前为止,所有版本的运算符都可以使用一个异常(exception)。指针读取操作符读取正确的数据,但未正确保存在 QList<*> 中。

这是一些示例代码。

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

派系.h
    #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。当代码
dataStream >> factions

运行时,应该读入 Faction* 的整个 QList。但是我得到了垃圾。
QDataStream & operator >>(QDataStream &dataStream, Faction *rhs)
{
rhs = new Faction();
return rhs->read(dataStream); <---- rhs will return good data.
}

rhs 包含从文件中读入的正确数据。但是,在读取了整个 QList 之后,QList 中存在垃圾值。

谁能帮我吗?

最佳答案

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

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

关于qt - QDatastream operator>> 用于 QList<Class*>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7381843/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com