Qt Serialization no match for operator << operand types are 'QDataStream'

683 views Asked by At

I have a problem during serialization with a simple Managed Structure.

My Class Header

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

and source cpp

   QDataStream &operator<<(QDataStream &s, const MappySaver::Tile& tile)
{
    s << tile;
    return s;
}

When I try to compile this, I get this error:

error: no match for 'operator<<' (operand types are 'QDataStream' and '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;
}

I use this class to serialize a list. On the main form, I have a widget with the same data.

And I use this method for try to serialize.

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();

How can I fix this problem and serialize?

0

There are 0 answers