gpt4 book ai didi

c++ - 调用不带对象参数的非静态成员函数,C++ 节点

转载 作者:行者123 更新时间:2023-11-28 05:57:27 25 4
gpt4 key购买 nike

这个网站似乎对此进行了深入介绍,但在搜索了一段时间后,我似乎无法找到具体的问题所在,因为我遇到了 void 函数的错误。

我正在尝试编写霍夫曼树代码,并且正在尝试构建最小堆。为此,我需要能够交换树中的节点。我在第一次调用 swapNode 的行中不断收到上述错误。

Node.cpp 违规代码

void Node::swapNode(Node &a, Node &b) {
Node t = a;
a = b;
b = t;
}
Node**minHeapify(Node **array, int index) {
int root = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (array[left]->frequency < array[root]->frequency) {
root = left;
}
if (array[right]->frequency < array[root]->frequency) {
root = right;
}
if(root != index) {
Node::swapNode(&array[root], &array[index]);
minHeapify(array, root);
}
}

节点.h

#ifndef _listnode_h
#define _listnode_h

#include <string>

class Node {
public:
Node(char character, int frequency, int ascii);
Node(int frequency);
Node();
~Node();
int getFrequency();
char getCharacter();
std::string getCode();
Node *getLeft();
Node *getRight();
Node **minHeapify(Node *, int);
void swapNode(Node &a, Node &b);
void setCode(std::string s);

// private:
int frequency;
char character;
int ascii;
std::string code;
int leaf = 0;
Node* left, *right;
};

#endif

我传入的数组是一个树节点数组,我想将其分类到最小堆中。感谢您提供任何帮助,谢谢。

最佳答案

如果你想调用 swapNode 而不是直接在实际对象上调用它(这是合理的)并且你希望它成为类成员,那么你需要将函数声明为静态的。或者你可以只使用 std::swap

关于c++ - 调用不带对象参数的非静态成员函数,C++ 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863463/

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