在阅读this tutorial和此stack overflow topic之后,我编写了以下代码来重载我写入此标题的位移
#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
实现为:
#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;
}
如果我在以下主页中使用它:
#include "Circle.h"
#include <QDebug>
int main(int argc, char *argv[])
{
Circle circle = Circle(2, QVector2D(3, 5));
qDebug() << circle;
}
我收到以下错误列表:
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(); }
...
在RobbieEs评论之后我将代码更改为
#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
不幸的是,我仍然遇到与之前相同的错误。
答案 0 :(得分:1)
感谢Jaa-c和RobbieE的评论我得到了它,最后我为两个流重载:
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