1

我正在制作一个编码类。它目前未能建立。我正在从一个函数中实例化一个映射,并收到错误消息“在抽象但具有非虚拟析构函数的 'Map::Comparator' 上调用的删除”。任何想法如何得到这个建立?

这是头文件;

#ifndef _encoding_
#define _encoding_

#include "bstream.h"
#include "vector.h"
#include "map.h"
#include "HuffmanTypes.h"
#include "pqueue.h"


class Encoding {

public:
    Encoding();
    ~Encoding();

    void compress(ibstream& infile, obstream& outfile);
    void decompress(ibstream& infile, obstream& outfile);
    //Map<ext_char, std::string> *referenceTable;
    std::string compresskey;
private:


    string filename;
    Map<ext_char, int> getFrequency(ibstream& infile);
    void createHuffman();
    //Map<ext_char, int> *frequencyTable;

    HuffmanPQueue huffqueue;
    //void buildTree();
    //void createReferenceTable();
    //Node getNode(Node root);
    //void tracePaths(Node root, string path, int weight);


};

#endif

这是cpp文件:

/**
 * File: encoding.cpp
 * ------------------
 * Place your Encoding class implementation here.
 */

#include "encoding.h"

#include "string.h"
#include "map.h"
#include "strlib.h"
#include "HuffmanTypes.h"
#include "pqueue.h"

using namespace std;

Encoding::Encoding() {

}

Encoding::~Encoding() {

}

void Encoding::compress(ibstream& infile, obstream& outfile) {

    Map<ext_char, int> frequencyTable;

    frequencyTable = getFrequency(infile);

    compresskey = "";


    foreach(ext_char key in frequencyTable) {

        int freq = frequencyTable.get(key);

        Node* newnode = new Node;
        newnode->character = key;
        newnode->weight = freq;
        newnode->zero = NULL;
        newnode->one = NULL;

        huffqueue.enqueue(newnode);
        string keystring = integerToString(key);
        string valstring = integerToString(freq);
        compresskey = compresskey + "Key = " + keystring + " " + "Freq = " + valstring + " ";

    }
    //buildTree();
    //createReferenceTable();

}

void Encoding::decompress(ibstream& infile, obstream& outfile) {

}

Map<ext_char, int> Encoding::getFrequency(ibstream& infile) {

    Map<ext_char, int> frequencyTable;

    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);

    return frequencyTable;
}
4

1 回答 1

1

编译器告诉你,抽象类 Map::Comparator 需要一个虚拟析构函数。如果通过指向基类的指针删除对象,则调用未定义的行为,前提是该基类不包含虚拟析构函数。

顺便说一句:我会为你的类省略空的构造函数和析构函数。如果我阅读您的类声明,看到析构函数,但没有复制构造函数和赋值运算符,我会感到紧张。

于 2013-05-19T11:36:39.773 回答