2011-09-11 67 views
4

因此,我已经为QDatastream的自定义类重载了>>和< <运算符。我做了每个版本的2个;一个用于基础对象,另一个用于对象的指针。到目前为止,所有版本的操作符都有一个例外。指针读取操作符读取正确的数据,但它不能正确保存在QList < *>中。QDatastream operator >> for QList <Class*>

下面是一些示例代码。

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

#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。当代码

运行,Faction *的整个QList应该被读入。但是我得到垃圾。

QDataStream & operator >>(QDataStream &dataStream, Faction *rhs) 
{ 
    rhs = new Faction(); 
    return rhs->read(dataStream); <---- rhs will return good data. 
} 

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

谁能帮助我吗?

回答

6

操作>>预计的引用作为其第二个参数,你可以有引用的指针太:

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

这是问题所在。所以我想确保我明白你改变了什么。因此,不是传递指针的“副本”,而是传递指针的引用,以便对该指针所做的任何更改都将转换为原始指针。那个听起来是对的吗? – jecjackal

+0

是的,就是这样。 – alexisdm

1

因为您想要改变rhs指向的内容,您应该将指针指针作为参数传递给您的操作符。

此外,对指针的更改会在函数返回时反映出来。

QDataStream & operator >>(QDataStream &dataStream, Faction **rhs) 
{ 
    *rhs = new Faction(); 
    return (*rhs)->read(dataStream); <---- rhs will return good data. 
} 
+0

我试过了,它不会编译,因为我的变种是的QList 的。我将不得不将其更改为QList ,这让我在同一条船上 – jecjackal

+0

由于我在返回语句中获得了很好的数据,我不知道序列化指针的QLists是否存在问题。 hmm – jecjackal

+0

你可以将你的指针添加到你的QList中,看看[this](http://ideone.com/rMoCS)的例子。我知道它与矢量,但想法是一样的。 –

相关问题