I've allocated a node an address within an encoding class by going
newnode.zero = &zeronode;
newnode and zeronode are instances of a node struct, which has a member pointer Node *zero. How do I access that node in a different function in the same class? At the moment I can only seem to get a pointer- by going
Node newnode = *root.zero;
Here's the whole encoding class as it stands right now;
/**
* 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;
}
How do I access that node in a different function in the same class?
Are you asking how to access a data member from within a member function? Just use it's name, zero
. If you like explicitness, you could say this->zero
.
Or are you asking how to get at newnode
from a function that doesn't know anything about it? You can't; you'll need to make it available to the other function somehow. How to do that depends on where you keep newnode
, and where you call the other function; we'll need more details to advise on that.
I can only seem to get a pointer- by going
Node newnode = *root.zero;
That's not a pointer, that's a copy. If you want a pointer, then:
Node * newnode = root.zero;
when we point pointer to structure then we should use -> operator to access the element of inside the Structure instead of . operator since you are using self referential Structure the inside element can be access like other normal element by using direct name of member or the this->node
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.