1

我在引用我试图在二进制堆实现的主文件中定义的方法时遇到编译器错误。报错都是指 buildTable 的方法声明所在行,报错如下:

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’

这是我的代码供参考:

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




    }

我在参数前面有无 std:: 和 huffnode:: 都试过了,都给我呈现了相同的错误消息。谢谢你的帮助。

4

2 回答 2

2

一个问题是参数声明

map table

您尚未向map该类提供模板参数。map与您曾经查看缺失内容的其他地方仔细检查。

于 2012-11-29T23:40:53.497 回答
2

首先,您可能想要更多类似的东西:

void buildTable(map<char, string> table, huffnode::huffnode * tree, std::string prefix){
    if(tree->leftChild() == NULL && tree->rightChild() == NULL){
        table[tree->getLetter()] = prefix;
    }
    else{
        buildTable(table, tree->leftChild(), prefix+"0");
        buildTable(table, tree->rightChild(), prefix+"1");
    }
}

那里有许多修复

  1. map<char, string> (specify template arguments)
  2. map[] -> table[]
  3. tree.getLetter() -> tree->getLetter()
  4. Missing } before else

I'm pretty sure the rest of the code will have as many problems. I can't tell them because, like huffnode, the definitions for heap (etc?) are missing.

I recommend taking it slowly and writing tiny bits of code, letting the compiler check your every step along the way, instead of writing a large body and then throwing it at a compiler at once.

Also, I assumed a huffnode definition like:

namespace huffnode 
{ 
    struct huffnode 
    { 
        huffnode *leftChild();
        huffnode *rightChild(); 
        char getLetter(); 
    }; 
}
于 2012-11-29T23:45:13.260 回答