C++ 导出类的重载运算符
我创建了一个具有以下结构的类(在Qt和Mingwin中是指定的情况):C++ 导出类的重载运算符,c++,export,overloading,operator-keyword,C++,Export,Overloading,Operator Keyword,我创建了一个具有以下结构的类(在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 { pub
#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
我必须做什么才能导出重载运算符并使用它们
感谢您的回复。您也必须在免费函数(本例中的运算符)中使用CALCULUSSHARED\u导出。这(我认为)只适用于Windows。我发现。。。我必须在运算符声明中编写类似的内容:演算::CartesianPoint2D CALCULUSSHARED_导出运算符+(常量演算::CartesianPoint2D&xAPoint,常量演算::CartesianPoint2D&xBPoint);不相关的注意:应该在
演算
名称空间(即定义类型的名称空间)中定义运算符。依赖于参数的查找将尝试在参数的名称空间中查找运算符,因此无需在根名称空间中将它们设置为全局运算符。通过将它们移动到同一名称空间,可以使操作符成为接口的一部分,这样可以避免名称空间污染,并且查找时由于歧义而发现错误重载/失败的机会也会减少。谷歌的界面原理。好的,我会把它们放进去。谢谢你的建议。
path\sources\testcalculus.cpp:273: error: undefined reference to `operator+(Calculus::CartesianPoint2D const&, Calculus::CartesianPoint2D const&)'