友元函数无法访问私有数据成员 (c++)
Friend function cannot access private data member (c++)
我搜索了许多不同的问题,但找不到与我的具体问题相匹配的解决方案。我有一个队列的头文件:
#ifndef HEADERFILE
#define HEADERFILE
#include <iostream>
#include <vector>
using namespace std;
template<class myType>
class Queue{
private:
int size;
vector<myType> list;
public:
Queue(int);
void Enqueue(myType);
myType Dequeue();
myType PeekFront();
int length();
void empty();
myType Index(int);
friend void printArray();
};
#endif
有问题的问题是 friend void printArray
。这是实现文件:
#include "queueTask1.h"
#include <vector>
#include <iostream>
using namespace std;
(Other function implementations)
void printArray(){
for (int i = 0; i < list.size(); i++){
cout << list.at(i) << ", ";
}
cout << endl;
}
尝试 运行 时的错误表明
'list' is not declared in this scope
但是它是在头文件中声明的,所有其他成员函数都可以正常工作。由于某种原因 printArray
找不到私有数据成员 list
,即使它应该是友元函数。
您需要将 class 实例传递给 printArray()
,然后您就可以访问该实例的私有成员。否则,printArray()
不知道使用哪个实例。
void printArray(Queue &myQueue){
for (int i = 0; i < myQueue.list.size(); i++){
cout << myQueue.list.at(i) << ", ";
}
cout << endl;
}
声明非常好,但是您正在处理这个 class 的哪个实例?如果你有 object.list
,list
是可以访问的,但只有 list
没有引用任何东西。传递 class 的一个实例,并使用它来访问 list
.
类似于:
void printArray(const Queue& object)
list
是非静态数据成员。这意味着每个对象都有一个 list
。由于它依赖于对象,因此您需要一个对象来访问它的 list
。最简单的方法是将对象传递给函数,如
// forward declare the function, unless you want to define the function inside the class
template<class ElementType>
friend void printArray(const Queue<ElementType>&);
template<class myType>
class Queue{
//...
// declare the friendship
template<class ElementType>
friend void printArray(const Queue<ElementType>&);
//...
};
// define the function
template<class ElementType>
void printArray(const Queue<ElementType>& q)
{
for (int i = 0; i < q.list.size(); i++){
cout << q.list.at(i) << ", ";
}
cout << endl;
}
您还需要将 Queue
的实现移动到头文件中,因为它是一个模板。有关更多信息,请参见:Why can templates only be implemented in the header file?
我自己,我会这样做:
template<class myType>
class Queue{
// snip ...
public:
// snip ...
template<class F>
friend void foreach_element(Queue& q, F&& f) {
for(auto&&e:list) f(e);
}
template<class F>
friend void foreach_element(Queue const& q, F&& f) {
for(auto&&e:list) f(e);
}
};
template<class myType>
void printArray(Queue<myType> const& q) {
foreach_element(q, [](auto&& e){ std::cout << e << ","; } );
std::cout << std::endl;
}
请注意,printArray
的实现必须放在 header 中,因为它是一个模板函数。
我公开了 foreach_element
以获取元素,然后让 printArray
成为使用它的 non-friend。
我搜索了许多不同的问题,但找不到与我的具体问题相匹配的解决方案。我有一个队列的头文件:
#ifndef HEADERFILE
#define HEADERFILE
#include <iostream>
#include <vector>
using namespace std;
template<class myType>
class Queue{
private:
int size;
vector<myType> list;
public:
Queue(int);
void Enqueue(myType);
myType Dequeue();
myType PeekFront();
int length();
void empty();
myType Index(int);
friend void printArray();
};
#endif
有问题的问题是 friend void printArray
。这是实现文件:
#include "queueTask1.h"
#include <vector>
#include <iostream>
using namespace std;
(Other function implementations)
void printArray(){
for (int i = 0; i < list.size(); i++){
cout << list.at(i) << ", ";
}
cout << endl;
}
尝试 运行 时的错误表明
'list' is not declared in this scope
但是它是在头文件中声明的,所有其他成员函数都可以正常工作。由于某种原因 printArray
找不到私有数据成员 list
,即使它应该是友元函数。
您需要将 class 实例传递给 printArray()
,然后您就可以访问该实例的私有成员。否则,printArray()
不知道使用哪个实例。
void printArray(Queue &myQueue){
for (int i = 0; i < myQueue.list.size(); i++){
cout << myQueue.list.at(i) << ", ";
}
cout << endl;
}
声明非常好,但是您正在处理这个 class 的哪个实例?如果你有 object.list
,list
是可以访问的,但只有 list
没有引用任何东西。传递 class 的一个实例,并使用它来访问 list
.
类似于:
void printArray(const Queue& object)
list
是非静态数据成员。这意味着每个对象都有一个 list
。由于它依赖于对象,因此您需要一个对象来访问它的 list
。最简单的方法是将对象传递给函数,如
// forward declare the function, unless you want to define the function inside the class
template<class ElementType>
friend void printArray(const Queue<ElementType>&);
template<class myType>
class Queue{
//...
// declare the friendship
template<class ElementType>
friend void printArray(const Queue<ElementType>&);
//...
};
// define the function
template<class ElementType>
void printArray(const Queue<ElementType>& q)
{
for (int i = 0; i < q.list.size(); i++){
cout << q.list.at(i) << ", ";
}
cout << endl;
}
您还需要将 Queue
的实现移动到头文件中,因为它是一个模板。有关更多信息,请参见:Why can templates only be implemented in the header file?
我自己,我会这样做:
template<class myType>
class Queue{
// snip ...
public:
// snip ...
template<class F>
friend void foreach_element(Queue& q, F&& f) {
for(auto&&e:list) f(e);
}
template<class F>
friend void foreach_element(Queue const& q, F&& f) {
for(auto&&e:list) f(e);
}
};
template<class myType>
void printArray(Queue<myType> const& q) {
foreach_element(q, [](auto&& e){ std::cout << e << ","; } );
std::cout << std::endl;
}
请注意,printArray
的实现必须放在 header 中,因为它是一个模板函数。
我公开了 foreach_element
以获取元素,然后让 printArray
成为使用它的 non-friend。