2013-05-15 1 views
0

@GManNickG이 (가) 작성한 코드 this에 관한 질문이 있습니다.친구 템플릿 기능에서 개인 회원에 액세스 할 수 없습니다.

정말 내가 (원래의 코드가 주석하고있다)과 같이 print_binary_helper의 친구 기능을 편집 무슨 일이 있었는지 이해하면 내가 볼 거라고

:

//template <typename U> 
//friend print_binary_helper<U> print_binary(U value); 
friend print_binary_helper<T> print_binary(T value); 

//template <typename U> 
//friend std::ostream& operator<<(std::ostream& sink, 
// const print_binary_helper<U> source); 
friend std::ostream& operator<<(std::ostream& sink, 
    const print_binary_helper<T> source); 

//template <typename U> 
//friend std::wostream& operator<<(std::wostream& sink, 
// const print_binary_helper<U> source); 
friend std::wostream& operator<<(std::wostream& sink, 
    const print_binary_helper<T> source); 

대신 U 만의 T를 사용하려면 프로그램이 컴파일되지 않습니다. 어떤 사람이 나에게 잘못한 것을 설명 할 수 있었습니까? 만약 이것이 가능하다면 어떻게 할 수 있습니까?

나는 VC++ (11)를 사용하고 있는데 이것은 내가 오류입니다 :

1>anything.cpp(68): error C2248: 'print_binary_helper<T>::print_binary_helper' : cannot access private member declared in class 'print_binary_helper<T>' 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>   anything.cpp(31) : see declaration of 'print_binary_helper<T>::print_binary_helper' 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>   anything.cpp(73) : see reference to function template instantiation 'print_binary_helper<T> print_binary<int>(T)' being compiled 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>anything.cpp(68): error C2248: 'print_binary_helper<T>::print_binary_helper' : cannot access private member declared in class 'print_binary_helper<T>' 
1>   with 
1>   [ 
1>    T=unsigned __int64 
1>   ] 
1>   anything.cpp(31) : see declaration of 'print_binary_helper<T>::print_binary_helper' 
1>   with 
1>   [ 
1>    T=unsigned __int64 
1>   ] 
1>   anything.cpp(75) : see reference to function template instantiation 'print_binary_helper<T> print_binary<unsigned __int64>(T)' being compiled 
1>   with 
1>   [ 
1>    T=unsigned __int64 
1>   ] 
+0

g에서 잘 작동합니다. 4.7.2 –

답변

1
template <typename U> 
friend print_binary_helper<U> print_binary(U value); 

템플릿print_binary 기능 친구를 만든다.

friend print_binary_helper<U> print_binary(U value); 

비 템플릿print_binary 기능 친구를 만든다.

두 가지가 다릅니다. 따라서 귀하의 경우 템플릿 기능은 친구가 아니며이고 템플릿 기능이 정의되어 있지 않습니다. 템플릿이 아닌 print_binary을 사용하지 않으므로 아무런 오류가 발생하지 않습니다.

기능은 친구입니다. 그래서 그들은 클래스의 템플릿 인자에 의존해서는 안됩니다. 그것들은 독립적 인 기능을 가져야한다.


당신이 함수를 선언 전송 한 다음 약간의 수정과 클래스에했던 것처럼 그들을 speicalize 수 print_binary_helper 클래스의 T 전문에 이러한 기능 친구의 T 전문을 확인하려면. 이 같은.

template <typename T> 
class print_binary_helper; 

template <typename T> 
std::ostream& operator<<(std::ostream& sink, 
         const print_binary_helper<T> source); 

template <typename T> 
std::wostream& operator<<(std::wostream& sink, 
          const print_binary_helper<T> source); 

template <typename T> 
print_binary_helper<T> print_binary(T value); 

template <typename T> 
class print_binary_helper 
{ 
public: 
    static_assert(std::is_integral<T>::value, 
        "Cannot print non-integer in binary."); 

    //make only print_binary<T> a friend to print_binary_helper<T> 
    friend print_binary_helper<T> print_binary<>(const T value); 
           //   ^^  

    friend std::ostream& operator<< <>(std::ostream& sink, 
           // ^^ 
             const print_binary_helper<T> source); 

    friend std::wostream& operator<< <>(std::wostream& sink, 
           // ^^ 
            const print_binary_helper<T> source); 

여기에 Example입니다.

+0

고마워요! 이것은 templated 클래스와 friend 함수를 사용하는 예를 통해 작업하는 동안 도움이되었습니다. 내가 가진 책이 시대에 뒤진 것 같아. – AutonomousApps

관련 문제