2013-05-15 81 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); 

使用的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

功能是朋友。所以他們不應該依賴於班級的模板論證。他們應該是獨立的職能。


如果你想只T專業化的那些功能的朋友來print_binary_helper類,你可以聲明轉發的功能,然後speicalize他們就像你在稍作修改你的類做的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

謝謝!這有助於我通過使用帶模板類的朋友函數的示例。我認爲我擁有的這本書已經過時了。 – AutonomousApps