1

After reading this tutorial and this stack overflow topic, among others, I wrote the following code to overload the bit shift I wrote this header

#ifndef CIRCLE_H
#define CIRCLE_H

#include <QVector2D>
#include <QDataStream>

class Circle
{
public:
    Circle(const float radius, const QVector2D centerPostion);

    friend QDataStream& operator <<(QDataStream& stream, const Circle& circle);

private:
    float radius;
    QVector2D centerPostion;
};

#endif // CIRCLE_H

which is implemented as:

#include "circle.h"

Circle::Circle(float radius, QVector2D centerPostion) :
    radius(radius),
    centerPostion(centerPostion) {}

QDataStream& operator <<(QDataStream& stream, const Circle& circle) {
    return stream << circle.radius << circle.centerPostion;
}

If I then use it in the following main:

#include "Circle.h"
#include <QDebug>

int main(int argc, char *argv[])
{
    Circle circle = Circle(2, QVector2D(3, 5));
    qDebug() << circle;
}

I get the following list of errors:

19:27:38: Running steps for project untitled...
19:27:38: Configuration unchanged, skipping qmake step.
19:27:38: Starting: "/usr/bin/make" 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -mmacosx-version-min=10.7 -Wall -W -fPIC -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../untitled -I. -I../../Qt/5.5/clang_64/lib/QtWidgets.framework/Headers -I../../Qt/5.5/clang_64/lib/QtGui.framework/Headers -I../../Qt/5.5/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I../../Qt/5.5/clang_64/mkspecs/macx-clang -F/Users/laura/Qt/5.5/clang_64/lib -o main.o ../untitled/main.cpp
../untitled/main.cpp:10:14: error: invalid operands to binary expression ('QDebug' and 'Circle')
    qDebug() << circle;
    ~~~~~~~~ ^  ~~~~~~
../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/qdebug.h:129:20: note: candidate function not viable: no known conversion from 'Circle' to 'const void *' for 1st argument; take the address of the argument with &
    inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
...

After RobbieEs comment I changed the code to

#include "circle.h"

Circle::Circle(float radius, QVector2D centerPostion) :
    radius(radius),
    centerPostion(centerPostion) {}

QDebug operator<<(QDebug dbg, const Circle &c) {
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "Hoi!";
    return dbg;
}

#ifndef CIRCLE_H
#define CIRCLE_H

#include <QVector2D>
#include <QD>


class Circle
{
public:
    Circle(const float radius, const QVector2D centerPostion);

    friend QDebug operator<<(QDebug dbg, const Coordinate &c);

private:
    float radius;
    QVector2D centerPostion;
};

#endif // CIRCLE_H

Unfortunately I still get the same errors as earlier.

Community
  • 1
  • 1
Laura
  • 168
  • 2
  • 9
  • According to [this](http://doc.qt.io/qt-5/debug.html#providing-support-for-the-qdebug-stream-operator), the function signature should be different. – Jaa-c Nov 22 '15 at 18:34
  • That gives the same errors, and it does not allow me to write to other streams such as qInfo(). – Laura Nov 22 '15 at 18:46
  • 1
    `QDebug` is not the same as `QDataStream` – RobbieE Nov 22 '15 at 19:52
  • @RobbieE that doesn't help much, should I use `QDebug` instead of `QDataStream`? Or am I doing something else wrong? – Laura Nov 22 '15 at 19:57
  • You need to overload the stream operator for `QDebug` as well, if you want to use both `QDebug` and `QDataStream`, they are independent classes that are not compatible. – RobbieE Nov 22 '15 at 20:16
  • Also, make sure to provide an `extern` declaration of `operator<<` in `circle.h`. – Davislor Nov 22 '15 at 20:33
  • Pass `QDebug` by reference, not by value, and return the reference. – RobbieE Nov 22 '15 at 20:40

1 Answers1

2

Thanks to Jaa-c and RobbieE comments I got it, in the end I overloaded for both streams:

circle.cpp:

#include "circle.h"

Circle::Circle(float radius, QVector2D centerPostion) :
    radius(radius),
    centerPostion(centerPostion) {}

QDebug operator<<(QDebug stream, const Circle &circle) {
    stream << "Circle[radius = "
           << circle.radius
           << ", centerPosition =  ["
           << circle.centerPostion.x()
           << ", "
           << circle.centerPostion.y()
           << "]]";
    return stream;
}

circle.h:

#ifndef CIRCLE_H
#define CIRCLE_H

#include <QVector2D>
#include <QDebug>
#include <QDataStream>


class Circle
{
public:
    Circle(const float radius, const QVector2D centerPostion);

    friend QDebug operator<<(QDebug stream, const Circle &circle);

private:
    float radius;
    QVector2D centerPostion;
};

#endif // CIRCLE_H
Laura
  • 168
  • 2
  • 9