正在访问指针地址处的节点
Accessing Node at a pointer address
我通过在编码类中为节点分配了一个地址
newnode.zero = &zeronode;
newnode和zeronode是节点结构的实例,该结构具有成员指针node*zero。如何访问同一类中不同函数中的节点?目前,我似乎只能通过获得指针
Node newnode = *root.zero;
这是目前的整个编码类;
/**
* File: encoding.cpp
* ------------------
* Place your Encoding class implementation here.
*/
#include "encoding.h"
#include "map.h"
#include "string.h"
#include <string>
#include "strlib.h"
#include "huffman-types.h"
#include "pqueue.h"
using namespace std;
Encoding::Encoding() {
}
Encoding::~Encoding() {
frequencyTable.clear();
}
void Encoding::compress(ibstream& infile, obstream& outfile) {
getFrequency(infile);
compresskey = "";
foreach(ext_char key in frequencyTable) {
int freq = frequencyTable.get(key);
Node newnode;
newnode.character = key;
newnode.weight = freq;
newnode.zero = NULL;
newnode.one = NULL;
huffqueue.enqueue(newnode, freq);
string keystring = integerToString(key);
string valstring = integerToString(freq);
compresskey = compresskey + "Key = " + keystring + " " + "Freq = " + valstring + " ";
}
buildTree();
createReferenceTable();
}
void Encoding::decompress(ibstream& infile, obstream& outfile) {
}
void Encoding::getFrequency(ibstream& infile) {
int ch;
while((ch = infile.get()) != EOF){
if(frequencyTable.containsKey(ch)){
int count;
count = frequencyTable.get(ch);
frequencyTable[ch] = ++count;
}
else {
frequencyTable.put(ch, 1);
}
}
frequencyTable.put(PSEUDO_EOF, 1);
}
void Encoding::buildTree() {
int numnodes = huffqueue.size();
for (int i = 1; i < numnodes; i++) {
Node newnode;
newnode.character = NOT_A_CHAR;
Node zeronode = huffqueue.extractMin();
newnode.zero = &zeronode;
Node onenode = huffqueue.extractMin();
newnode.one = &onenode;
int newfreq = zeronode.weight + onenode.weight;
newnode.weight = newfreq;
huffqueue.enqueue(newnode, newfreq);
}
}
void Encoding::createReferenceTable() {
Node root = huffqueue.extractMin();
string path = "";
tracePaths(root, path);
}
void Encoding::tracePaths(Node root, string path) {
if (!root.character == NOT_A_CHAR) {
ext_char ch = root.character;
referenceTable.put(ch, path);
return;
}
for (int i = 0; i < 2; i++) {
if (i == 0) {
if (root.zero != NULL) {
Node newnode = root->zero;// This is where the problem is
path = path + "0";
tracePaths(newnode, path);
}
}
else if (i == 1) {
if (root.one != NULL) {
Node newnode = root.one;
path = path + "1";
tracePaths(newnode, path);
}
}
}
return;
}
如何访问同一类中不同函数中的节点?
您是否在询问如何从成员功能中访问数据成员?只需使用它的名称zero
。如果你喜欢明确,你可以说this->zero
。
或者你在问如何从一个一无所知的函数中获得newnode
?你不能;您需要以某种方式将其提供给其他功能。如何做到这一点取决于newnode
的存放位置以及调用其他函数的位置;我们需要更多的细节来提供建议。
我似乎只能通过获得指针
Node newnode = *root.zero;
那不是指针,那是副本。如果你想要一个指针,那么:
Node * newnode = root.zero;
当我们将指针指向结构时,我们应该使用->运算符来访问structure内部的元素,而不是。运算符,因为您使用的是自引用结构,所以可以像其他正常元素一样,通过使用成员的直接名称或this->节点来访问内部元素
相关文章:
- 反向给定链表中的K节点
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Boost Graph Library,修复节点大小
- C++中是否有一个函数可以为您获取指向该节点的所有指针的地址空间
- 如何在NS3中设置节点的IP地址和数据?
- 在C++中设置节点的内存地址
- C++:尝试将新节点添加到链表会产生"线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)"错误
- 不同节点的相同地址
- 存储链接列表节点地址
- 通过池分配器中的内存地址访问可用列表节点时出现问题
- 为节点分配指针地址
- 获取类型为"节点"的临时对象的地址
- 正在访问指针地址处的节点
- 如何返回节点的地址
- 在第三次调用中,当地址因本地作用域而被销毁时,temp2->next 如何能够访问第二个节点的地址?
- 复制构造函数双链接列表中的头节点和尾节点的地址不正确
- 给定链表中的节点地址,获取节点的地址