I have a program designed with templates to implement a Huffman tree. My HuffmanNode class has two different data types. Here's the class implementation.
#pragma once
template <typename T, typename U> class HuffmanNode
{
private:
int frequency;
T value;
U code;
HuffmanNode<T, U>* left, *right;
public:
HuffmanNode<T, U>() :
value(' '), frequency(0), left(nullptr), right(nullptr) {}
HuffmanNode<T, U>(T v, U i) :
value(v), frequency(i), left(nullptr), right(nullptr) {}
HuffmanNode<T, U>(HuffmanNode<T, U>* left, HuffmanNode<T, U>* right)
{
this->left = left;
this->right = right;
frequency = left->getFrequency() + right->getFrequency();
value = NULL;
}
};
In my HuffTree class, I have a pointer pointing to a HuffmanNode pointer. I tried declaring the pointer to a HuffmanNode pointer a couple different ways, but either way, I get an error.
I declared it like this:
HuffmanNode<T>** storage; // Error: 'HuffmanNode': too few template arguments
Then I tried with both data types like this:
template <class T, class U>
HuffmanNode<T, U>** storage; // Error: 'HuffTree<T>::storage': only static data member templates are allowed
How would such a pointer be declared?
Edit: I updated the HuffTree class to include both data types in the HuffmanNode pointer, but now I get errors in my HuffmanCode class which includes the HuffTree class. Here's the updated code for the HuffTree class:
#pragma once
#include "HuffmanNode.h"
using namespace std;
template <typename T, typename U> class HuffTree
{
private:
HuffmanNode<T, U>** storage;
int treeSize;
int capacity;
public:
HuffTree<T, U>(int size)
{
capacity = size;
treeSize = 0;
storage = new HuffmanNode<T, U>*[capacity];
for (int i = 0; i < capacity; i++)
{
storage[i] = NULL;
}
}
// I get an error in the HuffmanCode class when calling this function
void insert(HuffmanNode<T, U>* rhs)
{
treeSize++;
storage[treeSize - 1] = rhs;
percUp(treeSize - 1);
}
};
Here's the code for the HuffmanCode class which gives me an error now that I fixed the pointer issue:
#pragma once
#include <string>
#include <map>
#include"HuffmanTree.h"
using std::string;
using std::map;
template <typename T, typename U> class HuffmanCode
{
private:
string data;
string encodedData;
HuffTree<T, U>* tree;
map<T, U> frequencyTable;
map<T, U> huffmanTable;
void buildTree()
{
for (map<T, U>::iterator it = frequencyTable.begin();
it != frequencyTable.end(); ++it)
{
/*
this line gives me an error: 'HuffmanNode<char,std::string>::
HuffmanNode(const HuffmanNode<char,std::string> &)':
cannot convert argument 1 from 'const std::string' to 'char'
*/
tree->insert(new HuffmanNode<char, string>(it->first, it->second));
}
}
public:
HuffmanCode<T, U>(T input)
{
data = input;
encodedData = "";
// create tree to be large enough for worst case
tree = new HuffTree<T, U>(data.length());
buildTable();
buildTree();
}
};
Here's my source file to test those functions:
#include <iostream>
#include <string>
#include "HuffmanCode.h"
using namespace std;
int main()
{
string text;
cout << "Enter some text:\n";
getline(cin, text);
HuffmanCode<string, string>* huffText = new HuffmanCode<string, string>(text);
huffText->displayTable();
huffText->displayHuffmanTable();
string code = huffText->getEncodedString();
cout << "Encoded string: " << code << endl << endl;
cout << "Decoded string: " << huffText->decodeString(code) << endl << endl;
delete huffText;
return 0;
}
Edited to include relevant code