Writing my own object to a binary file.



  • Hi,

    I want to write my own object to the binary file. But I receive the message:

    [QUOTE]C:\Qt\Qt5.1.1\5.1.1\mingw48_32\include\QtCore\qdatastream.h:300: error: no match for 'operator<<' (operand types are 'QDataStream' and 'const User')
    s << *it;
    ^[/QUOTE]

    [CODE]
    void Dialog::on_btnWrite_clicked()
    {
    QString nike = ui->wleNike->text();
    QString password = ui->wlePassword->text();
    QString firstName = ui->wleFirstName->text();
    QString lastName = ui->wleLastName->text();
    int age = ui->wspAge->text().toInt();

    User user;
    user.nike = nike;
    user.password = password;
    user.firstName = firstName;
    user.lastName = lastName;
    user.age = age;
    
    QVector<User> users;
    users.push_back(user);
    
    QString fileName = "c:/users.txt";
    QFile file&#40;fileName&#41;;
    
    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_5_1);
    
    out << users;
    
    file.flush();
    file.close();
    

    }
    [/CODE]

    Thank you!



  • Yes, you must define your own appropriate stream operators declared as non-member of the class

    @
    QDataStream &operator<<(QDataStream &, const User &);
    QDataStream &operator>>(QDataStream &, User &);
    @

    before you can use them.



  • Thank you! I'll try :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.