0

I got 2 errors of 'undefined reference to vtable for ...' when I'm practicing polymorphism in clang++ with c++11 with release mode. Below are some parts of my code.

#include <armadillo>
typedef float CHOSENONE;
typedef std::vector<std::vector<CHOSENONE> > fVect2d;
typedef std::vector<CHOSENONE> fVect;
class CfSubSet {
    private:
        fmat m_Data;
        frowvec m_Label;
        int m_NumInstances, m_NumFeatures ;
    public:
        CfSubSet(){
            cout<<"hello! \n" <<endl;
            m_NumInstances = 0;
            m_NumFeatures = 0;
            m_Data.zeros();
            m_Label.zeros();
        }
        CfSubSet(int iNumInstances, int iNumFeatures):m_NumInstances(iNumInstances),m_NumFeatures(iNumFeatures){
            m_Data.resize(iNumInstances,iNumFeatures);
            m_Label.resize(iNumInstances);
        }
        ~CfSubSet(){
            m_Data.reset();
            m_Label.reset();
        }
        inline int getNumInstances(){return m_NumInstances;};
        inline int getNumFeatures(){return m_NumFeatures;};
        inline fmat& getData(){return m_Data;};
        inline fmat& getLabel(){return m_Label;};
};

class CData
{
    protected:
        CfSubSet * m_Test;
        CfSubSet * m_Train;
    public:

        CData(){
            m_Test = new CfSubSet();
            m_Train = new CfSubSet();
        };

        ~CData(){
            delete m_Test;
            delete m_Train;
        };

        virtual void readDataFile(string ifFileTrainName) = 0;
        virtual void readLabelFile(string ifFileLabelFile) = 0;
        virtual void readTestFile(string ifFileTestFile) = 0;
        virtual void readGroundTruthFile(string ifGroundTruthFile) = 0;
};


class CMNISTSet : public CData
    {
    public:
        CMNISTSet() {};
        ~CMNISTSet(){};

        void readDataFile(string ifFileTrainName); 
        void readLabelFile(string ifFileLabelFile); 
        void readTestFile(string ifFileTestFile); 
        void readGroundTruthFile(string ifGroundTruthFile); 

    private:
        void ReadData(string ifFileName, fmat & rfOut); 
        void ReadLabel(string ifFileName, frowvec &rfOut); 
        int reverseInt (int i); 
    };

According to Undefined reference to vtable

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual

I have implemented all virtual functions, but in case of destructor, i didn't get the point because I also defined. Do I miss anything which must be in my code? Is there anyone can help. Thank you in advance.

UPDATE 1: I put my complete code in here. In MNISTSet.cpp

#include "MNISTSet.h"
int CMNISTSet::reverseInt (int i){
    unsigned char c1, c2, c3, c4;
    c1 = i & 255;
    c2 = (i >> 8) & 255;
    c3 = (i >> 16) & 255;
    c4 = (i >> 24) & 255;
    return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}

void CMNISTSet::ReadData(string ifFileName, fmat & rfOut){
    ifstream ifs(ifFileName.c_str(),std::ios::in | std::ios::binary);
    int magic_number = 0;
    int number_of_images = 0;
    int rows = 0;
    int cols = 0;

    ifs.read((char*)&magic_number,sizeof(magic_number));
    magic_number= reverseInt(magic_number);
    ifs.read((char*)&number_of_images,sizeof(number_of_images));
    number_of_images= reverseInt(number_of_images);
    ifs.read((char*)&rows,sizeof(rows));
    rows= reverseInt(rows);
    ifs.read((char*)&cols,sizeof(cols));
    cols= reverseInt(cols);

    rfOut = fmat(number_of_images, rows*cols);
    cout << magic_number << " " << number_of_images << " " << rows << " " << cols << endl;

    for(int i = 0; i < number_of_images; i++){

        for(int row = 0; row < rows; row++){
            for(int col = 0; col < cols; col++){
                unsigned char temp = 0;
                ifs.read((char*)&temp,sizeof(temp));
                rfOut(i,rows*row+col) = (CHOSENONE)temp;
            }
        }
    }
}

void CMNISTSet::readDataFile(string ifFileTrainName){
    ReadData(ifFileTrainName, this.m_Train->getData());
}

void CMNISTSet::readTestFile(string ifFileTestFile){
    ReadData(ifFileTestFile, this.m_Test->getData());
}

void CMNISTSet::ReadLabel(string ifFileName, frowvec & rfOut){
    ifstream ifs(ifFileName.c_str(),std::ios::in | std::ios::binary);
    int magic_number = 0;
    int number_of_images = 0;

    ifs.read((char*)&magic_number,sizeof(magic_number));
    magic_number= reverseInt(magic_number);
    ifs.read((char*)&number_of_images,sizeof(number_of_images));
    number_of_images= reverseInt(number_of_images);

    rfOut = frowvec(number_of_images);
    cout << number_of_images << endl;

    for(int i = 0; i < number_of_images; i++){
        unsigned char temp = 0;
        ifs.read((char*)&temp,sizeof(temp));
        rfOut(i) = (CHOSENONE)temp;
    }
}

void CMNISTSet::readLabelFile(string ifFileTestFile){
    ReadLabel(ifFileTestFile, m_Labels);
}

void CMNISTSet::readGroundTruthFile(string ifGroundTruthFile){
    ReadLabel(ifGroundTruthFile, m_GroundTruth);
}
Community
  • 1
  • 1
Bryan
  • 1,477
  • 1
  • 21
  • 38
  • The [`override` keyword](http://en.cppreference.com/w/cpp/language/override) is usually helpful in these kind of situations. Not sure but might be helpful here too. – Emil Laine Mar 21 '15 at 20:45
  • 3
    This is probably not the cause for the error, but [make the `CData` destructor `virtual`](http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors). – Emil Laine Mar 21 '15 at 20:50
  • 1
    Post a complete program that shows the problem. You didn't show that you have defined functions corresponding to the pure virtual methods, etc. – M.M Mar 21 '15 at 20:56
  • I have update the code, You can see I have define all of functions – Bryan Mar 22 '15 at 04:17
  • Did you mean C++11? C doesn't have polymorphism – M.M Mar 22 '15 at 05:31
  • I used c++11. I will edit. Thanks for correction – Bryan Mar 22 '15 at 05:33

1 Answers1

5

Your subclass function declarations must be defined somewhere.

To test, I changed your overrides declarations to:

void readDataFile(string ifFileTrainName) {}
void readLabelFile(string ifFileLabelFile) {}
void readTestFile(string ifFileTestFile) {} 
void readGroundTruthFile(string ifGroundTruthFile) {}

This works.

If you define the functions in a separate .cpp file. Then you need to specify their class.

void CMNISTSet::readDataFile( ... ){ ... }
...
davepmiller
  • 2,620
  • 3
  • 33
  • 61