导出类的重载运算符
我已经创建了(在 Qt 和 Mingwin 的指定情况下)一个具有以下结构的类:
#ifndef POINT2D_H
#define POINT2D_H
#include "Calculus_global.h"
#include <QtCore>
namespace Calculus
{
/** Class for definition of a point in 2D space */
class CALCULUSSHARED_EXPORT CartesianPoint2D
{
public:
//! Constructor
CartesianPoint2D();
//! Set x value
void setX(const qreal &qrX);
// ETC ETC ETC
};
} // namespace Calculus
///////////////////////////////////////////////////////////////////////////////
// RELATED NON-MEMBER OPERATORS //
///////////////////////////////////////////////////////////////////////////////
//! Addition operator
Calculus::CartesianPoint2D operator +(const Calculus::CartesianPoint2D &xAPoint, const Calculus::CartesianPoint2D &xBPoint);
//! Subtraction operator
Calculus::CartesianPoint2D operator -(const Calculus::CartesianPoint2D &xAPoint, const Calculus::CartesianPoint2D &xBPoint);
// And so on...
#endif // POINT2D_H
当我使用这个库时,类方法运行良好。但是,当我想使用运算符时,出现未定义的引用错误,例如:
path\sources\testcalculus.cpp:273: error: undefined reference to `operator+(Calculus::CartesianPoint2D const&, Calculus::CartesianPoint2D const&)'
我必须做什么才能导出重载的运算符并使用它们?
感谢您的回复。
I've created (in Qt and Mingwin the specified case), a class that has the following structure:
#ifndef POINT2D_H
#define POINT2D_H
#include "Calculus_global.h"
#include <QtCore>
namespace Calculus
{
/** Class for definition of a point in 2D space */
class CALCULUSSHARED_EXPORT CartesianPoint2D
{
public:
//! Constructor
CartesianPoint2D();
//! Set x value
void setX(const qreal &qrX);
// ETC ETC ETC
};
} // namespace Calculus
///////////////////////////////////////////////////////////////////////////////
// RELATED NON-MEMBER OPERATORS //
///////////////////////////////////////////////////////////////////////////////
//! Addition operator
Calculus::CartesianPoint2D operator +(const Calculus::CartesianPoint2D &xAPoint, const Calculus::CartesianPoint2D &xBPoint);
//! Subtraction operator
Calculus::CartesianPoint2D operator -(const Calculus::CartesianPoint2D &xAPoint, const Calculus::CartesianPoint2D &xBPoint);
// And so on...
#endif // POINT2D_H
When I use this library, class methods works well. But when I want to use an operator, I got the undefined reference error, for example:
path\sources\testcalculus.cpp:273: error: undefined reference to `operator+(Calculus::CartesianPoint2D const&, Calculus::CartesianPoint2D const&)'
What I must do to export also the overloaded operators and use them?
Thanks for your replies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也必须在自由函数(本例中为运算符)中使用 CALCULUSSHARED_EXPORT。 (我认为)这仅适用于 Windows。
You have to use CALCULUSSHARED_EXPORT in your free functions (the operators in this case) too. This (I think) is true only to Windows.