在使用简单的托管结构进行序列化时遇到问题。
我的班级标题
class MappySaver
{
public:
MappySaver();
//structure
struct Tile
{
public:
int ID;
int x,y,w,h;
QRect rect;
};
QList<Tile> Tiles;
QObject t;
Tile tile;
};
QDataStream& operator << (QDataStream& s, const MappySaver::Tile& tile);
QDataStream& operator >> (QDataStream& s, MappySaver::Tile& tile);
#endif // MAPPYSAVER_H
和源cpp
QDataStream &operator<<(QDataStream &s, const MappySaver::Tile& tile)
{
s << tile;
return s;
}
当我尝试编译它时,出现此错误:
错误:“ operator <<”不匹配(操作数类型为“ QDataStream”和“ const MappySaver :: Tile”)
template <typename T> //QDataStream.h
QDataStream& operator<<(QDataStream& s, const QList<T>& l)
{
s << quint32(l.size());
for (int i = 0; i < l.size(); ++i)
s << l.at(i);
return s;
}
我使用此类来序列化列表。 在主窗体上,我有一个具有相同数据的小部件。
我使用这种方法尝试序列化。
QString filename = QFileDialog::getSaveFileName(this,
tr("Seleziona il file da salvare"),"",
tr("File Mappa (*.mp2d)"));
MappySaver::Tile t;
MappySaver m;
for(int i = 0; i < ui->maps->Tiles.count(); i++)
{
t.ID = ui->maps->Tiles[i].ID;
t.h = ui->maps->Tiles[i].h;
t.rect = ui->maps->Tiles[i].rect;
t.w = ui->maps->Tiles[i].w;
t.x = ui->maps->Tiles[i].x;
t.y = ui->maps->Tiles[i].y;
m.Tiles.append(t);
}
QFile f(filename);
if(!f.open(QIODevice::WriteOnly))
{
}else
{
QDataStream out(&f);
out.setVersion(QDataStream::Qt_4_8);
out << m.Tiles;
}
f.flush();
f.close();
如何解决此问题并进行序列化?