c++ - C++朋友关键字不访问非静态数据成员
问题描述
我需要ostream
使用双重链接的跳过列表类的新功能来重载运算符。
当我计算我的类的实例时,我希望它遍历我的跳过列表的级别,并且无论head
指针指向哪里,nullptr
我都希望它打印级别名称和空状态。
看起来像:
After adding 7
Level: 4 -- empty
Level: 3 -- empty
Level: 2 -- empty
Level: 1 -- empty
Level: 0 -- 7
我需要动态输入的级别数。我尝试分配int level = SkipList::maxLevels_;
,但出现错误invalid use of non-static data member
我已经做了ostream
a friend
。我如何指示它访问maxLevels_
数据成员?
跳过列表.h
#include <stdio.h>
#include <iostream>
#ifndef SKIP_LIST_
#define SKIP_LIST_
using namespace std;
class SkipList
{
private:
struct SkipListNode {
// Convenience constructor to create node, set its data, and set all pointers to nullptr
explicit SkipListNode(int data){
data_ = data;
next_ = NULL;
prev_ = NULL;
upLevel_ = NULL;
downLevel_ = NULL;
}
// data for SNode
int data_;
// link to next at same level
SkipListNode* next_;
// link to previous at same level
SkipListNode* prev_;
// link up one level
SkipListNode* upLevel_;
// link down one level
SkipListNode* downLevel_;
};
// maximum # of levels of SkipList, levels are 0 to maxLevels-1
int maxLevels_;
// array of maxLevels_ SkipListNode pointers as head pointers. For example,
// if maxLevels_ == 2, we'd have Heads[0] and Heads[1]. Dynamically allocated
// by constructor.
SkipListNode** heads_;
// array of maxLevels_ SkipListNode pointers as tail pointers.
SkipListNode** tails_;
// given a pointer to a SkipListNode, place it before the given nextNode
void addBefore(SkipListNode* newNode, SkipListNode* nextNode, int level);
// return true 50% of time,
// each node has a 50% chance of being at higher level
bool alsoHigher() const;
public:
//Constructor
SkipList(){maxLevels_ = 1;}
SkipList(int maxLevels);
//Destructor
// virtual ~SkipList();
// return true if successfully added, no duplicates
bool insert(int item);
// item deletion; return true if successfully removed
bool erase(int item);
// return true if found in SkipList
bool contains(int item) const;
friend ostream& operator<<(ostream& os, const SkipList& list){
int level = SkipList::maxLevels_;
while (level >= 0) {
SkipListNode* temp = list.heads_[level];
if (temp == nullptr) {
os << "Level: " << level << "-- empty";
}
else {
while (temp) {
os << temp->data_ << " ";
temp = temp->next_;
}
}
os << endl;
level--;
}
}
};
#endif
解决方案
SkipList::maxLevels_;
指类的静态maxLevels_
成员SkipList
。因此,如果您需要maxLevels_
成为所有实例的最高级别,则SkipList
必须将其声明为static
. 否则,在您重载的朋友函数中,您必须使用list
实例的私有成员。
friend ostream& operator<<(ostream& os, const SkipList& list){
int level = list.maxLevels_;
...
推荐阅读
- postgresql - 如何使用 U0000DGF 的起始值更新 postgres 中序列的所有行并在 postgres 中递增
- clojure - clojure(script) 最佳实践中的协程和处理异步?
- php - 如何从特定键迭代数组?
- python - 将视频从链接上传到 youtube
- amazon-web-services - 如何使用 terraform 添加将多个 aws 目标组添加到多个自动缩放组
- typescript - 开玩笑,我如何模拟一个导出的函数来返回一个 Promise 而不是 undefined?
- python - python在标题下方创建一个意外的空白行
- amazon-web-services - 子网中没有实例的负载均衡器会报错吗?
- django - 如何解决 pg_dump:由于使用 docker 的服务器版本不匹配而中止
- azure-logic-apps - 在 Logic 应用程序中使用 Teams 连接器时出错 - 无法连接 global.consent.azure-apim.net 或 logic-apis-westerurope.consent.azure-apim.net