mercredi 1 mai 2019

Is there a way to create different type but same base inheritance objects defined in a shared library?

I am trying to create a shared library to use for some of my projects. The idea is to contain all classes that I use for a generic role, in this case file I/O, with an API and create instances of derived classes tailored to specific tasks. The problem is that I can't create the different instances from the different derived classes. The compiler throws errors of "multiple definition of BaseFile::create(FileObjectType)" and "multiple definition of BaseFile::destroy(BaseFile*)"

I work with linux, and Code::Blocks. I have made a header file where the API (the base class where all others inherit from) and two functions, one to create and return an instance, and one to destroy it. At a second header file, I keep the class declarations and the implementation of the member functions create and destroy, and multiple files where I implement each class.

//This is the API header file
#ifndef FILELIB_HPP_INCLUDED
#define FILELIB_HPP_INCLUDED
#include <string>
#include <fstream>
using namespace std;
//The different classes available, that inherit the base class
enum FileObjectType {
    FOT_OUTFILE, FOT_OUTFILEB, FOT_INFILE, FOT_INFILEB
};
//The base class
class BaseFile {
protected:
    string sFileName;
    int iSize;
    fstream FileStream;
public:
    virtual bool bOpenFile(string FileName);
    virtual string sReturnBlock(int IndexNumber, int BlockSize);
    virtual bool bAppendBlock(string BlockToWrite);
    virtual bool bCreateFile(string FileName);
        virtual void vCloseFile();
        //The two functions that return 
        static BaseFile* create(FileObjectType);
        static void destroy(BaseFile* prt);
    BaseFile() {};
    virtual ~BaseFile();
};
//The two functions to be exported
extern "C" BaseFile* create(FileObjectType eType);
extern "C" void destroy(BaseFile* ptr);

#endif // FILELIB_HPP_INCLUDED

The main header file where I keep the declarations:

//The header file where I keep the declarations
#ifndef MAINHEADER_HPP_INCLUDED
#define MAINHEADER_HPP_INCLUDED
#include "FileLib.hpp"
#include <iostream>
//Each specialised class declarations
class OutFile : public BaseFile {
private:
public:
    OutFile();
    ~OutFile();
};
class OutFileb : public BaseFile {
private:
public:
    OutFileb();
    ~OutFileb();
};
class InFileb : public BaseFile {
private:
public:
    InFileb();
    ~InFileb();
};
class InFile : public BaseFile {
private:
public:
    InFile();
    ~InFile();
};
//The two functions-to-be-exported implementation
BaseFile* BaseFile::create(FileObjectType eType)
{
    if (eType == FOT_OUTFILE) return new OutFile;
    if (eType == FOT_OUTFILEB) return new OutFileb;
    if (eType == FOT_INFILE) return new InFile;
    if (eType == FOT_INFILEB) return new InFileb;
    return NULL;
}
void BaseFile::destroy(BaseFile* ptr)
{
    delete ptr;
    return;
}
#endif // MAINHEADER_HPP_INCLUDED

The error of multiple definitions says that the functions create and destroy were first defined here. I've tried to declare those functions outside the BaseFile class, as standalone functions, but the same error remains.

The implementation files contain the #include for the main header file, the empty implementations of the derived classes' constructors and destructors, and nothing else. I tried to implement the factory method. Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire