Eric Reis Figueiredo
Eric Reis Figueiredo

Reputation: 289

QT << overload not working QT5.3

I`m trying to overload the << operator for a client object:

void Login::saveClient(Client *client)
{
    this->file = new QFile(CLIENTS_FILE);
    this->file->open(QIODevice::Append | QIODevice::Text);
    QTextStream out(this->file);
    out << client;
}

In my Client.h I have:

class Client : public QObject
{
    Q_OBJECT

public:
    Client(QString username);

    friend QDataStream & operator <<(QDataStream &s, Client *c);

    QString getUsername();
}

In my Client.cpp:

Client::Client(QString username)
{
    this->username = username;
}

QDataStream & operator <<(QDataStream &s, Client *c)
{
    s << c->getUsername();
    return s;
}

QString Client::getUsername()
{
    return this->getUsername();
}

But in the file i get the pointer`s adress like:

0x135551c7c0

0x13534aa480

Can anyone help me with this?

With the help of moosingin3space (thanks very much) and some adaptation I managed to make this work. I Had to change the QDataStream to QTextStream.

Thank you all !

Upvotes: 1

Views: 287

Answers (1)

moosingin3space
moosingin3space

Reputation: 326

When you define the << operator, it should be defined like so:

QDataStream& operator<<(QDataStream& s, const Client& c)

The streaming operators in C++ are designed to work on references, not pointers.

Upvotes: 4

Related Questions