我为我的应用程序定义了三个类:int2_
(夫妇整数),float2_
(夫妇夫妇)和double2_
(夫妻双打),基本上执行复杂的算术运算。超载运算符=两个以上类别的对象之间
我想超载上述三个类的对象之间的赋值运算符=
。动机是我正在编写CUDA代码,但似乎没有为CUDA类int2
,float2
和double2
定义的赋值运算符。
我目前实行的是以下几点:
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
};
一切都归纳为东西不提供编译错误。剩余的操作员过载分别返回以下错误
error : identifier "float2_" is undefined
error : identifier "double2_" is undefined
error : identifier "double2_" is undefined
分别针对三种不同的过载。
由于例如float2_
和double2_
尚未在int2_
类定义之前定义,所以似乎存在“可见性”问题。有关如何解决问题的任何建议?非常感谢你提前。
解决方案以下列出JAMES甘孜的答案,阿里克斯·莱德
这里是解决方案:
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; }
在类int2_中,您的类型float2_,double2_尚未定义。要使用它们,请在类int2声明前插入“class float2_; class double2_;”而不是实例,将引用作为运算符的参数传递。详细了解前向声明。 – Serhiy
@Serhly他们甚至没有宣布。但我认为他知道这一点;我认为这就是他的“可见性”问题。 –
@Serhiyi谢谢Serhiyi,我已经意识到这一点。因此我提到有一个“可见性”问题。无论如何,我修改我的帖子是更具体的。 – JackOLantern