|
이단자 님이 쓰신 글 : : 첨부파일에 보면 템플릿클래스가 있습니다. : 연산자를 보면 다른 타입의 템플릿클래스를 받아서 자신것으로 바꿀수 있도록 되어 있습니다. : : VC++에서는 잘 되는데, BCB에서는 컴파일 에러가 나는군요. BCB에서는 불가능한가요? : : : 소스코드가 다 입력이 안되어서 첨부파일로 넣었습니다.
올려주신 소스가 이건데요
template <class T> class AAA { public: AAA(const AAA<T>& rhs); AAA<T>& operator = (AAA<T>& rhs);
template<class U> AAA(const AAA<U>& rhs);
template <class U> AAA<T>& operator = (const AAA<U>& rhs); };
template<class T> inline AAA<T>::AAA(const AAA<T>& rhs) { }
template <class T> template <class U> inline AAA<T>::AAA(const AAA<U>& rhs) { }
template <class T> template <class U> inline AAA<T>& AAA<T>::operator = (const AAA<U>& rhs) { }
template<class T> inline AAA<T>& AAA<T>::operator=(AAA<T>& rhs) { }
우선 어디서 에러가 나신다는건지 모르겠지만 제가보기엔 당연히 컴파일이 안될 것으로 보입니다. 최신 Ansi C++ 문법이 어떤지는 모르겠지만 template<class T> template<class U> inline ~~
이러는건 문법상 안맞을거 같은데요 template<class T, class U> 이런식으로 시작해야 할거 같은데요
그리고 다른타입의 객체(다른 클래스가 되겠죠)를 받고 싶으시다면 예제가 맞을지 모르겠지만
#include <stdio.h> #include <iostream>
template<class T> class TEMP { public: T value; public: TEMP(const TEMP<T> &rhs) : value(rhs.value) {} TEMP(const T _value) : value(_value) {}
template<class U> TEMP(const TEMP<U> &rhs) : value(rhs.value) {} };
int main() { TEMP<double> t1(10.0); TEMP<int> t2(5); TEMP<double> t3(t2); // 다른타입의 클래스를 복사생성자의 인자로 받음 cout << t1.value << endl << t2.value << endl << t3.value << endl; getchar();
return 0;
}
이런식이 되어야 맞을거 같네요. 정확하게 의도하신 목적을 말씀해주셨다면 확실히 대답해 드렸을텐데. 틀리다면 답변달아주시길 바랍니다. ^^; 그럼 좋은하루 보내세요
|