我对@GManNickG 编写的这段代码有疑问。
我打算看看如果我真的理解发生了什么,所以我编辑了这样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 而不是 U 但程序不会编译。有人可以向我解释我做错了什么吗?如果这是可能的,如果是的话,怎么办?
我正在使用 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> ]