Mike Caron
Mike Caron

Reputation: 14561

C++ virtual function not found

I have a class designed to do import/export of data in one of a few different formats. Each format should have exactly the same interface, so I'm implementing it as a base class with a bunch of virtual methods and a derived class for each specific format:

#ifndef _IMPORTEXPORT_H_
#define _IMPORTEXPORT_H_

#include "stdio.h"

enum EXPORT_TYPE {
    EXPORT_INI = 1,
};

class exportfile {
public: 
    virtual ~exportfile();

    static exportfile * openExportFile(const char * file, EXPORT_TYPE type);

    virtual void startSection(int id) = 0;
    virtual void endSection() = 0;

protected:
    exportfile(const char * file);
    FILE * hFile;

};

class iniexportfile : public exportfile {
public:
    iniexportfile(const char * file) : exportfile(file) { }

    void startSection(int id);
    void endSection();
private:
    bool inSection;

};

#endif

This is the base class (exportfile) and one of the derived classes (iniexportfile).

The implementation of these methods are as such:

#include "importexport.h"

#include <exception>
#include <assert.h>

exportfile * openExportFile(const char * file, EXPORT_TYPE type) {
    switch(type) {
        case EXPORT_INI:
            return new iniexportfile(file);
        default:
            return NULL;
    }
}

exportfile::exportfile(const char * file) {

        this->hFile = fopen(file, "w");

        if(this->hFile == 0) {
            throw new std::exception("Unable to open export file");
        }
}

exportfile::~exportfile() {
    assert(this->hFile != 0);

    this->endSection();

    fclose(this->hFile);
    this->hFile = 0;
}

void iniexportfile::startSection(int id) {
    assert(this->hFile != 0);

    fprintf(this->hFile, "[%d]\r\n", id);

    this->inSection = true;
}

void iniexportfile::endSection() {
    this->inSection = false;
}

(Note, the class is obviously not complete.)

Finally, I have a test method:

#include "importexport.h"

#include <exception>

using namespace std;

void runImportExportTest() {
    iniexportfile file("test.ini");

    file.startSection(1);

    file.endSection();
}

Anyway, this all compiles fine, but when it gets linked, the linker throws up this error:

error LNK2001: unresolved external symbol "public: virtual void __thiscall exportfile::endSection(void)" (?endSection@exportfile@@UAEXXZ)   importexport.obj

Why is it looking for exportfile::endSection(), when it's marked as pure virtual? Did I somehow not make it pure virtual? Or, have I been spoiled by C#, and am totally mucking these virtual functions up?

Incidentally, this is Visual Studio 2008. Thought I should mention that somewhere.

Upvotes: 1

Views: 1721

Answers (1)

Michael Burr
Michael Burr

Reputation: 340208

By the time this dtor gets called:

exportfile::~exportfile() {
    assert(this->hFile != 0);

    this->endSection();

    fclose(this->hFile);
    this->hFile = 0;
}

the compiler has 'unwound' the vtable so it will resolve to the exportfile::endSection() function - it will not call the derived version. You'll need to design a different clean up method.

See "Never call virtual functions during construction or destruction".

Upvotes: 2

Related Questions