int2_
(정수 쌍), float2_
(부동 소수점 쌍) 및 double2_
(복소수 쌍)은 본질적으로 복잡한 연산 연산을 수행하기 위해 3 가지 클래스를 정의합니다 .오버로딩 연산자 = 두 클래스 이상의 객체 간
위의 세 클래스의 개체간에 할당 연산자 =
을 오버로드하려고합니다. 동기는 내가 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_
클래스 정의 전에 아직 정의되지 않았기 때문에 "가시성"문제가있는 것으로 보입니다. 문제 해결 방법에 대한 제안? 대단히 감사드립니다. 제임스 간제의 답변 앨릭스 라이더 여기
이 솔루션을 파악하기
해결책 :
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; }
int2_ 클래스에서 float2_, double2_ 유형이 아직 정의되지 않았습니다. 그것들을 사용하기 전에 클래스 int2 선언 앞에 "class float2_; class double2_;"를 삽입하십시오. 인스턴스 대신에 참조를 연산자의 인수로 전달하십시오. 앞으로 선언문에서 더 읽으십시오. – Serhiy
@Serhly 그들은 선언조차하지 않았습니다. 그러나 나는 그가 그것을 알고 있다고 생각한다; 나는 그가 "가시성"문제에 의해 의미하는 것이라고 생각합니다. –
@ Serhiyi Serhiyi에게 감사드립니다. 이미 알고 있습니다. 따라서 나는 "가시성"문제가 있다고 언급했다. 어쨌든, 좀 더 구체적으로 내 게시물을 수정했습니다. – JackOLantern