2013-04-13 31 views
1

我为我的应用程序定义了三个类:int2_(夫妇整数),float2_(夫妇夫妇)和double2_(夫妻双打),基本上执行复杂的算术运算。超载运算符=两个以上类别的对象之间

我想超载上述三个类的对象之间的赋值运算符=。动机是我正在编写CUDA代码,但似乎没有为CUDA类int2float2double2定义的赋值运算符。

我目前实行的是以下几点:

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; } 
+0

在类int2_中,您的类型float2_,double2_尚未定义。要使用它们,请在类int2声明前插入“class float2_; class double2_;”而不是实例,将引用作为运算符的参数传递。详细了解前向声明。 – Serhiy

+0

@Serhly他们甚至没有宣布。但我认为他知道这一点;我认为这就是他的“可见性”问题。 –

+0

@Serhiyi谢谢Serhiyi,我已经意识到这一点。因此我提到有一个“可见性”问题。无论如何,我修改我的帖子是更具体的。 – JackOLantern

回答

2

首先,你需要使用正类的声明:

class Int2; 
class Float2; 
class Double2; 

然后,在类中,您只应声明函数,而不是 实现它们。这些实现应该在类别定义的全部后面,无论是在一个单独的源文件中,还是显式声明为内联的 。

+0

非常感谢。这个建议奏效了。我编辑我的帖子来说明最终的解决方案。一个问题:“内联”是什么意思?我应该内联运算符'=',还是你的意思是“在同一个文件中”?再次,谢谢。 – JackOLantern

+0

谢谢。我已将“inline”关键字添加到我的最终解决方案中。我认为如果没有必要,它会被编译器忽略,对吧? – JackOLantern

0

问题是float2_仅在int2_之后定义,它已被使用。 添加这两个类的向前声明和你应该罚款:

class int2_; 
class double2_; 
+0

如果他在类定义中保留实现,则不是。在实现这些功能时,他需要查看完整的类定义。 –

+0

我按照你的建议添加了前面的声明:'class int2_;','class float2_;'和'class double2_;',在我的帖子中公开的代码之前。不幸的是,所有这三个错误现在都变成'不完整的类型是不允许的'。 – JackOLantern

1

您需要如下更改类声明的顺序。
向前声明不会在这种情况下帮助,因为你需要类型声明在操作中使用=

class double2_ { 

    public: 
     double x; 
     double y; 

     double2_() : x(), y() {} 

     // Stuff 
}; 

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 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; } 
}; 

也有与运营商的问题=本身,它应该返回*本;而不是在操作员=完成工作后才会超出范围。

+0

我无法逆转类声明的顺序,例如,对于'double2_'类,“stuff”包含'double2_'和'int2_'和'float2_'之间的赋值:我之前应该提到过。因此,您的建议不能解决问题。但是我很欣赏返回'* this'的建议,这对于做出多重任务非常重要,例如'a = b = c;'。我用最后的解决方案编辑了我的帖子,其中也包含了您的建议。再次,谢谢。 – JackOLantern