Question: Need help correcting a error in c++ code it says no match for operator= (operand types are std::vector and brace) it happens in main. This
Need help correcting a error in c++ code it says no match for operator= (operand types are std::vector
main:
#include
#include "huffnode.h" #include "huffman.h"
using namespace std;
int main() { string characters; vector
huffman huff1;
huff1.buildTree(characters, charFreq);
huff1.generateCodes();
huff1.listCodes();
huff1.writeTree();
string charStr, bitCode; charStr = "end"; //bitCode = huff1.Compress(charStr); cout << "Compress" << endl; cout << charStr << " = " << bitCode << endl << endl;
bitCode = "100010101010101000"; //charStr = huff1.Decompress(bitCode); cout << "Decompress" << endl; cout << bitCode << " = " << charStr << endl << endl;
//Keep Console window open until keyboard input int hold; cin >> hold; }
huffman.h:
#ifndef HUFFMAN_H_INCLUDED #define HUFFMAN_H_INCLUDED
#include
#include "huffnode.h"
using namespace std;
class huffman {
public:
huffman();
void buildTree(const string& characters, const vector
private:
int numChars; int numLeaves; int treeSize;
//Huffman tree is stored in a vector of huffNode's vector
//String of characters used to generate the Huffman tree string characters; //Corresponding frequencies for the characters vector
int rootNodeIndex; vector
bool encode(char character, stringstream &ss, string binary, int currentNodeIndex); char decode(int currentNodeIndex, string bitsLeft); string substr(string str, int startIndex, int endIndex); };
huffman::huffman() { numChars = 0; numLeaves = 0; treeSize = 0; charLoc.resize(256); }
void huffman::buildTree(const string& chars, const vector
// Put all nodes in a vector for reference purposes for (unsigned i = 0; i < chars.length(); i++) { huffNode node; node.index = i; node.freq = freqs[i]; node.ch = chars[i]; tree.push_back(node); }
// We put all nodes now in a priority queue priority_queue
for (unsigned i = 0; i < tree.size(); i++) huffPQ.push(tree[i]);
// We now build the huffman tree... // At the end there should only be one node in the queue... while (huffPQ.size() > 1) { huffNode leftNode = huffPQ.top(); huffPQ.pop();
huffNode rightNode = huffPQ.top(); huffPQ.pop();
// Build a new node that points to the node having their frequencies combined huffNode node; node.freq = leftNode.freq + rightNode.freq; node.left = (short)leftNode.index; node.right = (short)rightNode.index; node.ch = '\0'; node.index = (int)tree.size(); tree.push_back(node);
tree[leftNode.index].parent = node.index; tree[rightNode.index].parent = node.index;
huffPQ.push(node); }
// The last item in the queue is the root node, we're done building the tree rootNodeIndex = huffPQ.top().index; }
// Helper method to convert a character to binary bool huffman::encode(char character, stringstream &ss, string binary, int currentNodeIndex) { if (tree[currentNodeIndex].ch != '\0') { if (tree[currentNodeIndex].ch == character) { // Finally, found the character in the tree ss << binary; tree[currentNodeIndex].numBits = (int) binary.length(); return true; } else { // Nope, looks like the character does not exist in the tree return false; } }
// Find the character on the left branch if (encode(character, ss, binary + "0", tree[currentNodeIndex].left)) { return true; }
// If we haven't found the character on left branch, try right branch // If we haven't found the character to any branch, then we cannot encode return encode(character, ss, binary + "1", tree[currentNodeIndex].right); }
// Helper method to find the chaaracter given the bits left char huffman::decode(int currentNodeIndex, string bitsLeft) { if (currentNodeIndex < 0 || currentNodeIndex >= (int) tree.size()) { // Looks like no more nodes to traverse... return '\0'; }
if (bitsLeft.empty()) { // No more bits to traverse, return whatever we have in the node return tree[currentNodeIndex].ch; }
// We still have bits left, depending on the bit value // we go left or right if (bitsLeft[0] == '0') { bitsLeft.erase(bitsLeft.begin()); return decode(tree[currentNodeIndex].left, bitsLeft); }
bitsLeft.erase(bitsLeft.begin()); return decode(tree[currentNodeIndex].right, bitsLeft); }
void huffman::generateCodes() { // Generate the huffman code for each characters for (unsigned i = 0; i < characters.length(); i++) { stringstream ss;
if (encode(characters[i], ss, "", rootNodeIndex)) charCodes.push_back(ss.str()); else charCodes.push_back(""); } }
void huffman::listCodes() { cout << "HuffmanCodes" << endl;
for (unsigned i = 0; i < characters.length(); i++) cout << characters[i] << " " << charCodes[i] << endl; }
void huffman::writeTree() { cout << "Huffman Tree" << endl;
cout << setw(7) << left << "Index"; cout << setw(5) << left << "ch"; cout << setw(7) << left << "freq"; cout << setw(7) << left << "left"; cout << setw(9) << left << "right"; cout << setw(9) << left << "parent"; cout << setw(10) << left << "numBits"; cout << "bitCode"; cout << endl;
for (unsigned i = 0; i < tree.size(); i++) { cout << setw(7) << left << i;
if (tree[i].ch == '\0') cout << setw(5) << left << ""; else cout << setw(5) << left << tree[i].ch;
cout << setw(7) << left << tree[i].freq; cout << setw(7) << left << tree[i].left; cout << setw(9) << left << tree[i].right; cout << setw(9) << left << tree[i].parent; cout << setw(10) << left << tree[i].numBits;
if (tree[i].ch == '\0') { cout << ""; } else { for (unsigned j = 0; j < characters.length(); j++) { if (characters[j] == tree[i].ch) { cout << charCodes[j]; break; } } }
cout << endl; } }
string huffman::Compress(string str) { string bits = "";
for (unsigned i = 0; i < str.length(); i++) { for (unsigned j = 0; j < characters.size(); j++) { if (str[i] == characters[j]) { bits += charCodes[j]; break; } } }
return bits; }
// Get the substring of a string string huffman::substr(string str, int startIndex, int endIndex) { stringstream ss;
for (int i = startIndex; i < endIndex; i++) ss << str[i];
return ss.str(); }
string huffman::Decompress(string code) { string str = ""; string bits = code;
while (!bits.empty()) { bool bitsUpdated = false;
for (unsigned endIndex = 1; endIndex <= bits.length(); endIndex++) { string attemptBits = substr(bits, 0, endIndex); char decodedChar = decode(rootNodeIndex, attemptBits);
if (decodedChar != '\0') { str += decodedChar; bits = substr(bits, endIndex, (int) bits.length()); bitsUpdated = true; } }
if (!bitsUpdated) break; }
return str; }
#endif // HUFFMAN_H_INCLUDED
huffNode.h:
#ifndef HUFFNODE_H_INCLUDED #define HUFFNODE_H_INCLUDED
#include
using namespace std;
//NIL represents empty subtree const short NIL = -1;
class huffNode { public:
unsigned char ch; short left, right; int freq; int index; int parent; int numBits; string bitCode;
huffNode();
friend bool operator> (huffNode lhs, huffNode rhs); };
huffNode::huffNode() { ch = 0; left = NIL; right = NIL; freq = 0; index = 0; parent = 0; numBits = 0; bitCode = ""; }
bool operator>(huffNode lhs, huffNode rhs) { return lhs.freq > rhs.freq; }
#endif // HUFFNODE_H_INCLUDED
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
