2010-07-18 18 views
1

템플릿이기도 한 클래스에 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(); 
} 
+0

이 아닌'연산자 <<()'하나 개의 매개 변수를 취해야한다 : –

+1

@klez - 아니요,'operator <<'는 여기있는 자유 함수이므로 두 개의 매개 변수가 필요합니다. –

+0

@R Samuel Klatchko, 완벽한, 이제는 C++을 잘 모른다는 확신이 들었습니다. D –

답변

0

당신이 클래스 t_zstring의 < < 연산자를 정의하려면

, 나는 쉽게 작성하는 것입니다 생각하십니까?
+0

OS의 종류는 std :: basic_ostream이 될 것입니다. –

+0

그래, 맞아. g ++ 4.2.1이 깨질 수 있다고 제안 했으므로, 더 쉬운 선언으로 해결할 수 있다고 생각했습니다.결국 OS를 std :: basic_ostream으로 제한하는 것이 항상 필요한 것은 아닙니다. –