Robbie Cooper
Robbie Cooper

Reputation: 483

How to instantiate Map within a class?

I'm making an encoding class. its currently failing to build. I'm instantiating a map from within a function and am getting the error message "Delete called on 'Map::Comparator' that is abstract but has non-virtual destructor". Any ideas how to get this to build?

Here's the header file;

#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

Here's the cpp file:

/**
 * 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;
}

Upvotes: 1

Views: 449

Answers (1)

Torsten Robitzki
Torsten Robitzki

Reputation: 2555

The compiler is telling you, that the abstract class Map::Comparator needs a virtual destructor. If you delete an object through a pointer to base class, you invoke undefined behavior, if that base class contains no virtual destructor.

BTW: I would omit the empty constructor and destructor for your class. I would get nervous if I read your class declaration, seeing a destructor, but no copy constructor and no assignment operator.

Upvotes: 1

Related Questions