I’m getting a compiler error in reference to a method I am trying to define in the main file of a binary heap implementation. The errors all refer to the line where the method declaration for buildTable is, and the errors are as follows:
lab4.cpp:9: error: variable or field ‘buildTable’ declared void
lab4.cpp:9: error: missing template arguments before ‘table’
lab4.cpp:9: error: expected primary-expression before ‘*’ token
lab4.cpp:9: error: ‘tree’ was not declared in this scope
lab4.cpp:9: error: expected primary-expression before ‘prefix’
Here’s my code for reference:
#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include "heap.h"
#include "huffnode.h"
using namespace std;
void buildTable(map table, huffnode::huffnode * tree, std::string prefix){
if(tree->leftChild() == NULL && tree->rightChild() == NULL){
map[tree.getLetter()] = prefix;
else{
buildTable(table, tree->leftChild(), prefix+"0");
buildTable(table, tree->rightChild(), prefix+"1");
}
}
int main() {
map<char, int> code; // Constructs map <letter, frequency>
map<char,int>::iterator it;
map<char,string>::iterator it2;
map<char, string> encoding;
string input;
getline(cin, input);
for(int i = 0; i < input.length(); i++){ // build a map from the string
char currLetter = input[i];
int exist = code.count(currLetter);
//cout << exist << endl;
if(exist == 0){ // check if letter already has a key
code[currLetter] = 1;
}else{
code[currLetter] = code[currLetter] + 1;
}
if(currLetter == '.'){
break;
}
}
heap binHeap;
for (it=code.begin() ; it != code.end(); it++){ // Fill the heap
huffnode * newHuff = new huffnode((*it).first, (*it).second);
cout << newHuff->getLetter() << " => " << newHuff->getFreq() << endl;
binHeap.insert(newHuff);
}
/*
while(binHeap.getSize() > 0){
cout << binHeap.extractMin()->getFreq() << endl;
}
*/
while(binHeap.getSize() > 1){ // build the tree
huffnode *newLeft, *newRight;
newLeft = binHeap.extractMin();
newRight = binHeap.extractMin();
huffnode * newInternal = new huffnode(newLeft, newRight);
binHeap.insert(newInternal);
}
huffnode * root = binHeap.extractMin();
buildTable(encoding, root, "");
for (it2=encoding.begin() ; it2 != encoding.end(); it2++){ // Fill the heap
cout <<it2->first << " => " << it2->second << endl;
}
}
I tried it with and without the std:: and huffnode:: in front of the parameters, both rendered me the same error message. Thanks for any help.
One problem is with the parameter declaration
You have not provided template parameters to the
mapclass. Double-check this with the other places where you usedmapto see what is missing.