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.