0

我已经通过去为一个节点分配了一个编码类中的地址

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;
}
4

2 回答 2

2

如何在同一类的不同函数中访问该节点?

您是否在问如何从成员函数中访问数据成员?就用它的名字,zero. 如果你喜欢明确,你可以说this->zero

或者你是在问如何newnode从一个对它一无所知的函数中获取信息?你不能;您需要以某种方式使其可用于其他功能。如何做到这一点取决于您保留newnode的位置以及调用其他函数的位置;我们需要更多细节来提供建议。

我似乎只能得到一个指针——通过

Node newnode = *root.zero;

那不是指针,那是副本。如果你想要一个指针,那么:

Node * newnode = root.zero;
于 2013-05-17T14:02:17.103 回答
0

当我们指向结构的指针时,我们应该使用 -> 运算符来访问结构内部的元素,而不是 . 运算符,因为您使用的是自引用结构,因此可以通过使用成员的直接名称或 this->node 像其他普通元素一样访问内部元素

于 2013-05-17T14:09:27.807 回答