PDA

View Full Version : Writing my own object to a binary file.



8Observer8
1st December 2013, 05:39
Hi,

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


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



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

QDataStream out(&file);
out.setVersion(QDataStream::Qt_5_1);

out << users;

file.flush();
file.close();
}


Thank you!

ChrisW67
1st December 2013, 08:15
You need to provide the operator<<() and operator>>() implementation for your User class. Its prototype looks like:


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

Each should do whatever is necessary to save/restore the internal state of the User object.