2013-05-29 6 views
3

출력 스트림 연산자 <<을 템플릿 클래스 정의 외부로 오버로드하려고합니다.템플릿 외부의 템플릿 클래스 오버로드 출력 스트림 연산자

템플릿 클래스의 내부를 구현하는 것은 괜찮습니다 :

template 
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>> 
class MyContainer : public Policy<T> 
{ 
public: 
    MyContainer():p(_MaxSize){}; 
    friend std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj); 
private: 
    Container p; 
}; 

template 
<typename T,int _MaxSize,template <class C> class Policy,typename Container> 
std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj) 
{ 
}; 

컴파일러가 불평 :

warning: friend declaration ‘std::ostream& operator<<(std::ostream&, MyContainer<T, _MaxSize, Policy, Container>)’ declares a non-template function [-Wnon-template-friend] 
tempstruct.cc:39:97: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 

template 
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>> 
class MyContainer : public Policy<T> 
{ 
public: 
    MyContainer():p(_MaxSize){}; 
    std::ostream& operator<<(MyContainer<T,_MaxSize,Policy,Container>& obj){ }; 
private: 
    Container p; 
}; 

하지만 템플릿 클래스 외부에서 작업을 수행하려고 할 때 누구나 어떻게 출력 스트림을 오페라로 할 수 있는지에 대한 간단한 예제를 줄 수 있습니까? 템플릿 클래스 외부에서 <<을 정의 했습니까?

관련 게시물에 내가 여기있는 모든 사람들이 템플릿 클래스 내에서 그것을 발견했다.

+2

잠시 생각해보십시오. "정말로 친구 일 필요가 있습니까?" – BoBTFish

+0

@BoBTFish 답변에 대한 확신이 없습니다. 필자는 템플릿이 아닌 클래스에서 친구로 선언했습니다. –

+0

'private' 멤버 데이터 나 함수를 사용합니까? 해야합니까? – BoBTFish

답변

5

아무에게도 출력 스트림 연산자 < <을 템플릿 클래스 외부에서 정의 할 수있는 간단한 예제를 제공 할 수 있습니까?

아니오, 간단하지 않기 때문입니다. 모두가 템플릿 클래스 안에 그것을 내가 여기에있는 관련 게시물에

// Declare the class template, because we need it to declare the operator 
template <typename,int,template <class C> class,typename> class MyContainer; 

// Declare the operator, because (as the error says) templates must be declared 
// in the namespace before you can declare them friends. At this point, we can't 
// define the operator, since the class template is incomplete. 
template 
<typename T,int _MaxSize,template <class C> class Policy,typename Container> 
std::ostream& operator<<(std::ostream&,MyContainer<T,_MaxSize,Policy,Container>); 

// Define the class template 
template 
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>> 
class MyContainer : public Policy<T> 
{ 
public: 
    MyContainer():p(_MaxSize){}; 

    // Include <> to indicate that this is the template 
    friend std::ostream& operator<< <>(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj); 
private: 
    Container p; 
}; 

// Finally define the operator 
template 
<typename T,int _MaxSize,template <class C> class Policy,typename Container> 
std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj) 
{ 
    // print some stuff 
}; 

: 나는 복잡한 예제를 제공 할 수 있습니다.

나는 그렇게 할 것이다. 훨씬 더 간단합니다. 또는 컨테이너의 내용에 대한 충분한 액세스를 제공한다고 가정 할 때 공개 인터페이스와 관련하여 출력을 구현할 수도 있습니다. 그렇다면 친구 선언이 필요하지 않으므로 앞으로 선언 할 필요도 없습니다.

+0

마이크 대단히 감사합니다. 그게 내가 원하는거야. 알 겠어. –

관련 문제