템플릿이기도 한 클래스에 operator <<()
을 오버로드하고 싶습니다. 다음과 같이 내 클래스는 다음과 같습니다연산자 <<() 템플릿 클래스
template<
typename RefCountType,
typename TraitsType = std::char_traits<char>,
typename Allocator = std::allocator<typename TraitsType::char_type>
>
class rep {
// ...
};
template<typename RepType>
class t_zstring {
// ...
};
operator<<()
의 코드는 다음과 같습니다
template<
typename CharType,
typename TraitsType,
typename Allocator,
typename RefCountType,
template<class,class,class> class RepType
>
inline std::basic_ostream<CharType,TraitsType>&
operator<<(std::basic_ostream<CharType,TraitsType> &os,
t_zstring< RepType<RefCountType,TraitsType,Allocator> > const &s) {
return os << s.c_str();
}
템플릿 코드가 잘 컴파일; 그러나, 내가하려고 할 때 (생략 my_ref_count
클래스에 대한 코드)처럼 사용 :
typedef rep<my_ref_count> my_rep;
typedef t_zstring<my_rep> zstring;
zstring z;
cout << z; // ztest.cpp, line 201
을 내가 (g ++ 4.2.1 사용) 수 :
ztest.cpp:201: error: no match for ‘operator<<’ in ‘std::cout << z1’
가 어떻게 내 operator<<()
를 선언 할 수 있습니다 올바르게 컴파일러가 정확한 일치를 찾을 수 있도록?
template<typename OS, typename RepType>
OS& operator<<(OS &os, t_zstring<RepType> const &s)
{
return os << s.c_str();
}
이 아닌'연산자 <<()'하나 개의 매개 변수를 취해야한다 : –
@klez - 아니요,'operator <<'는 여기있는 자유 함수이므로 두 개의 매개 변수가 필요합니다. –
@R Samuel Klatchko, 완벽한, 이제는 C++을 잘 모른다는 확신이 들었습니다. D –