2013-04-13 2 views
1

int2_ (정수 쌍), float2_ (부동 소수점 쌍) 및 double2_ (복소수 쌍)은 본질적으로 복잡한 연산 연산을 수행하기 위해 3 가지 클래스를 정의합니다 .오버로딩 연산자 = 두 클래스 이상의 객체 간

위의 세 클래스의 개체간에 할당 연산자 =을 오버로드하려고합니다. 동기는 내가 CUDA 코드를 작성하고 있지만, CUDA 클래스 int2, float2double2에 대해 이미 정의 된 할당 연산자가없는 것 같습니다.

나의 현재 구현은 다음과 같다 :

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_ 클래스 정의 전에 아직 정의되지 않았기 때문에 "가시성"문제가있는 것으로 보입니다. 문제 해결 방법에 대한 제안? 대단히 감사드립니다. 제임스 간제의 답변 앨릭스 라이더 여기

이 솔루션을 파악하기

해결책 :

class Int2; 
class Float2; 
class Double2; 

:

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

첫째, 당신은 클래스의 선언을 앞으로 사용할 필요가 그런 다음 클래스에서 함수를 선언해야합니다 ( 은 해당 함수를 구현하지 않음). 구현은 별도의 소스 파일 또는 명시 적으로 인라인으로 선언 된 중 하나 인 클래스 정의 모두를 따라야합니다.

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

는 또한 운영자에 문제가 =이 경우 도움이되지 않습니다 = 자체가, 그것은 반환해야 *이; 작업자가 작업을 수행 한 직후에 범위를 벗어날 b 대신 b.

+0

예를 들어,'double2_' 클래스의 경우 "stuff"는'double2_'와'int2_' 및'float2_' 사이의 할당을 포함하기 때문에 클래스 선언의 순서를 뒤집을 수 없습니다. 따라서 귀하의 제안으로 문제가 해결되지는 않습니다. 그러나 '* this'를 반환하는 것이 좋습니다. 이는 다중 할당을 만들기 위해 매우 중요합니다 (예 :'a = b = c;'). 나는 당신의 제안을 포함하는 최종 해결책으로 나의 포스트를 편집했다. 다시 고마워. – JackOLantern