gpt4 book ai didi

c++ - 好友模板函数无法访问私有(private)成员

转载 作者:行者123 更新时间:2023-11-28 07:37:12 26 4
gpt4 key购买 nike

我有一个关于 this 的问题@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> ]

最佳答案

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

制作一个模板 print_binary 函数 friend 。

friend print_binary_helper<U> print_binary(U value);

创建一个非模板 print_binary 函数 friend 。

两者不同。所以在你的情况下模板函数是不是 friend 并且未定义非模板函数。你不会得到任何错误,因为你没有在任何地方使用非模板 print_binary

函数是 friend 。所以他们不应该依赖于类的模板参数。它们应该是独立的函数。


如果您只想使这些函数的 T 特化成为 print_binary_helper 类的 T 特化的 friend ,您可以转发声明函数和然后像你在类里面所做的那样,稍作修改,将它们专门化。像这样的事情。

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

关于c++ - 好友模板函数无法访问私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16562744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com