有没有一种方法可以创建共享库中定义的不同类型但相同的基本继承对象?

时间:2019-05-02 02:11:27

标签: c++ c++11

我正在尝试创建一个共享库以用于我的某些项目。想法是使用API​​包含我用于一般角色的所有类(在本例中为文件I / O),并创建为特定任务量身定制的派生类的实例。问题是我无法从不同的派生类创建不同的实例。编译器抛出“ BaseFile :: create(FileObjectType)的多个定义”和“ BaseFile :: destroy(BaseFile *)的多个定义”的错误

我使用linux和Code :: Blocks。我制作了一个头文件,其中包含API(所有其他继承该基类的基类)和两个函数,一个用于创建和返回实例,另一个用于销毁它。在第二个头文件中,我保留类声明以及成员函数的实现创建和销毁,并保留多个文件来实现每个类。

//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 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

多个定义的错误表明创建和销毁函数首先在此处定义。 我试图将BaseFile类之外的那些函数声明为独立函数,但是仍然存在相同的错误。

实现文件包含主头文件的#include,派生类的构造函数和析构函数的空实现,以及其他所有内容。我试图实现工厂方法。谢谢您的宝贵时间。

编辑:编译器的确切错误:(抱歉,文本转储网站无法正常工作,所以我也将其转储到这里)https://pastebin.com/ZGXNBQhW

-------------- Build: Debug in FileLib (compiler: GNU GCC Compiler)---------------

g++ -shared -L/usr/lib/i386-linux-gnu obj/Debug/InFile.o obj/Debug/InFileb.o obj/Debug/main.o obj/Debug/OutFileb.o -o bin/Debug/libFileLib.so
obj/Debug/InFileb.o: In function `OutFile::~OutFile()':
/home/myuser/Projects/FileLib/MainHeader.hpp:42: multiple definition of `BaseFile::create(FileObjectType)'
obj/Debug/InFile.o:/home/myuser/Projects/FileLib/MainHeader.hpp:42: first defined here
obj/Debug/InFileb.o: In function `BaseFile::destroy(BaseFile*)':
/home/myuser/Projects/FileLib/MainHeader.hpp:50: multiple definition of `BaseFile::destroy(BaseFile*)'
obj/Debug/InFile.o:/home/myuser/Projects/FileLib/MainHeader.hpp:50: first defined here
obj/Debug/main.o: In function `OutFile::~OutFile()':
/home/myuser/Projects/FileLib/MainHeader.hpp:42: multiple definition of `BaseFile::create(FileObjectType)'
obj/Debug/InFile.o:/home/myuser/Projects/FileLib/MainHeader.hpp:42: first defined here
obj/Debug/main.o: In function `BaseFile::destroy(BaseFile*)':
/home/myuser/Projects/FileLib/MainHeader.hpp:50: multiple definition of `BaseFile::destroy(BaseFile*)'
obj/Debug/InFile.o:/home/myuser/Projects/FileLib/MainHeader.hpp:50: first defined here
obj/Debug/OutFileb.o: In function `OutFile::~OutFile()':
/home/myuser/Projects/FileLib/MainHeader.hpp:42: multiple definition of `BaseFile::create(FileObjectType)'
obj/Debug/InFile.o:/home/myuser/Projects/FileLib/MainHeader.hpp:42: first defined here
obj/Debug/OutFileb.o: In function `BaseFile::destroy(BaseFile*)':
/home/myuser/Projects/FileLib/MainHeader.hpp:50: multiple definition of `BaseFile::destroy(BaseFile*)'
obj/Debug/InFile.o:/home/myuser/Projects/FileLib/MainHeader.hpp:50: first defined here
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
12 error(s), 0 warning(s) (0 minute(s), 0 second(s))

0 个答案:

没有答案