我對@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> ]
在g ++上正常工作4.7.2 –