Windows 코드를 Linux로 이식하는 동안 GCC 4.2.3에서 다음 오류 메시지가 나타납니다. (예, 나는 약간의 이전 버전 있다고 알고 있어요,하지만 난 쉽게 업그레이드 할 수 없습니다.) 나는이 오류를 생성하려면 다음 코드를 사용하고C++ 연산자 오버로드 - 클래스에서 캐스팅
main.cpp:16: error: call of overloaded ‘list(MyClass&)’ is ambiguous
/usr/include/c++/4.2/bits/stl_list.h:495: note: candidates are: std::list<_Tp, _Alloc>::list(const std::list<_Tp, _Alloc>&) [with _Tp = unsigned char, _Alloc = std::allocator<unsigned char>]
/usr/include/c++/4.2/bits/stl_list.h:484: note: std::list<_Tp, _Alloc>::list(size_t, const _Tp&, const _Alloc&) [with _Tp = unsigned char, _Alloc = std::allocator<unsigned char>]
.
#include <list>
class MyClass
{
public:
MyClass(){}
operator std::list<unsigned char>() const { std::list<unsigned char> a; return a; }
operator unsigned char() const { unsigned char a; return a; }
};
int main()
{
MyClass a;
std::list<unsigned char> b = (std::list<unsigned char>)a;
return 0;
}
이 오류가 발생 했습니까? 더 중요하게, 그것의 주위에 얻는 방법? (그것은 완전히 GetList()
등 기능과 같은 GetChar()
를 사용하여, 확인, 과부하를 피하기 위해 가능하지만, 나는 그것을 방지하고 싶습니다.)
(그런데, "operator unsigned char()
는"오류를 제거하는 제거.)
왜 그렇습니까? 당신이 암시 적으로 오버로드 된 캐스팅을 호출 할 수 있다는 사실을 알고 있습니다. –
죄송합니다. 명시 적으로 전화하십시오. –
확실히 모르겠다. 명시 적으로 operator를 (캐스팅하여) 사용하고 싶다고 가정하지 않는다면, 먼저 표준 연산자를 사용하여 const & then 연산자를 사용할 수도 있지만, 직접 연산자를 명시 적으로 사용하려는 경우 const &와 충돌하지만 실제로는 그렇지 않습니다. –