在两个以上类的对象之间重载operator = [英] Overloading operator= between objects of more than two classes
问题描述
我为我的应用程序定义了三个类: int2 _
(整数对), float2 _
浮动)和 double2 _
(双双),主要用于执行复杂的算术运算。
I'm defining three classes for my application: int2_
(couples of integers), float2_
(couples of floats) and double2_
(couples of doubles), essentially to perform complex arithmetics operations.
我想重载上述三个类的对象之间的赋值 =
运算符。动机是我编写一个CUDA代码,但似乎没有为CUDA类定义的赋值运算符 int2
, float2
和 double2
。
I want to overload the assignment =
operator between objects of the above three classes. The motivation is that I'm writing a CUDA code, but it seems that there is no assignment operator already defined for the CUDA classes int2
, float2
and double2
.
我目前的实作方式如下:
My current implementation is the following:
class int2_ {
public:
int x;
int y;
int2_() : x(), y() {}
// Stuff
const int2_& operator=(const float2_ a) { int2_ b; b.x = (int)a.x; b.y = (int)a.y; return b; }
const int2_& operator=(const double2_ a) { int2_ b; b.x = (int)a.x; b.y = (int)a.y; return b; }
};
class float2_ {
public:
float x;
float y;
float2_() : x(), y() {}
// Stuff
const float2_& operator=(const double2_ a) { float2_ b; b.x = (float)a.x; b.y = (float)a.y; return b; }
};
class double2_ {
public:
double x;
double y;
double2_() : x(), y() {}
// Stuff
};
一切都被归纳为 stuff 不提供编译错误。剩余的操作符重载返回以下错误:
Everything is summarized as stuff does not provide compilation errors. The remaining operator overloads return the following errors
error : identifier "float2_" is undefined
error : identifier "double2_" is undefined
error : identifier "double2_" is undefined
分别为三个不同的重载。
respectively for the three different overloads.
似乎存在一个可见性问题,因为例如 float2 _
在 int2 _
类定义之前尚未定义code> double2 _ 。任何建议如何解决问题?非常感谢。
It seems that there is a "visibility" problem due to the fact that, for example, float2_
and double2_
are not yet defined before the int2_
class definition. Any suggestion on how solving the problem? Thank you very much in advance.
解决方案按照JAMES KANZE和ALEXRIDER的答案
这里是解决方案:
class int2_;
class float2_;
class double2_;
class int2_ {
public:
int x;
int y;
int2_() : x(), y() {}
inline const int2_& operator=(const int a) { x = a; y = 0.; return *this; }
inline const int2_& operator=(const float a) { x = (int)a; y = 0.; return *this; }
inline const int2_& operator=(const double a) { x = (int)a; y = 0.; return *this; }
inline const int2_& operator=(const int2_ a) { x = a.x; y = a.y; return *this; }
inline const int2_& operator=(const float2_ a);
inline const int2_& operator=(const double2_ a);
};
class float2_ {
public:
float x;
float y;
float2_() : x(), y() {}
inline const float2_& operator=(const int a) { x = (float)a; y = 0.; return *this; }
inline const float2_& operator=(const float a) { x = a; y = 0.; return *this; }
inline const float2_& operator=(const double a) { x = (float)a; y = 0.; return *this; }
inline const float2_& operator=(const int2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
inline const float2_& operator=(const float2_ a) { x = a.x; y = a.y; return *this; }
inline const float2_& operator=(const double2_ a);
};
class double2_ {
public:
double x;
double y;
double2_() : x(), y() {}
inline const double2_& operator=(const int a) { x = (double)a; y = 0.; return *this; }
inline const double2_& operator=(const float a) { x = (double)a; y = 0.; return *this; }
inline const double2_& operator=(const double a) { x = a; y = 0.; return *this; }
inline const double2_& operator=(const int2_ a) { x = (double)a.x; y = (double)a.y;return *this; }
inline const double2_& operator=(const float2_ a) { x = (double)a.x; y = (double)a.y;return *this; }
inline const double2_& operator=(const double2_ a) { x = a.x; y = a.y; return *this; }
};
inline const int2_& int2_::operator=(const float2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
inline const int2_& int2_::operator=(const double2_ a) { x = (int)a.x; y = (int)a.y; return *this; }
inline const float2_& float2_::operator=(const double2_ a) { x = (float)a.x; y = (float)a.y; return *this; }
推荐答案
首先,类:
class Int2;
class Float2;
class Double2;
。实现应该在所有的
类定义之后,或者在单独的源文件中,或者
显式声明为inline。
Then, in the classes, you should only declare the functions, not implement them. The implementations should come after all of the class definitions, either in a separate source file, or explicitly declared inline.
这篇关于在两个以上类的对象之间重载operator =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!