C++从DLL实例化模板类

Cri*_*sti 2 c++ dll templates function-pointers

我试图制作一个包含以下内容的DLL:

基本模板类,只有虚拟析构函数而没有属性(我称之为MatrixInterface)

带有构造函数,析构函数,运算符=和属性(矩阵类)的派生类

一个返回基类指针到新派生对象的函数:

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

template<class T>
MatrixInterface<T> DLL_EXPORT * CreateMatrixInstance(unsigned int n,unsigned int m)
{
    return new matrix<T>(n,m);
}
Run Code Online (Sandbox Code Playgroud)

我想使用这个函数在我的程序中实现矩阵类,但我不能为这个函数分配一个函数指针,我不明白为什么.我可以通过这种方式加载任何不是模板函数的函数.

#include <windows.h>
#include <iostream>
using namespace std;

template<class T>
class MatrixInterface
{
public:
    virtual ~MatrixInterface(void);
};


typedef MatrixInterface<int>* (*Fptr)(unsigned int,unsigned int);

int main(int argc, char* argv[])
{
    Fptr p;
    MatrixInterface<int> *x;
    char path[]="basicmatrix.dll";
    HINSTANCE hDll = LoadLibrary(path);
    cout<<(char*)path<<endl;
    if(hDll)
    {
        cout<<"Library opened succesfully!"<<endl;
        p = (Fptr)GetProcAddress(hDll,"CreateMatrixInstance");
        if(p) {
            cout<<"working!\n";
            x=p(7,8);
            cout<<"MatrixCreated"<<endl;
            delete x;

        } else {
            cout<<"Failed loading function CreateMatrixInstance\n";
        }
    }
    else
    {
        cout<<"Failed loading library "<<(char*)path<<endl;
    }
    system("pause");
    FreeLibrary(hDll);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

基类存在于DLL和可执行文件中.


由于某种原因,Visual Studio无法打开DLL(使用MSVC或MinGW编译).我用MinGW编译了程序,并加载了.dll文件.


你能告诉我我的代码有什么问题吗?

Ste*_*and 8

模板仅在编译时解决!它们将在两个不同的编译单元中成为不同的类型.(这就是为什么用std::stringas参数导出函数真的很危险).

作为一个结果,您应该明确地将模板实例化为您将要使用/允许使用的类型.

在您的exportimport.h文件中,应该存在您要在dll中公开的所有类型的模板实例化.即MatrixInterface<int>.

你应该写:

template class MatrixInterface<int>;
Run Code Online (Sandbox Code Playgroud)

以便暴露一种且只有一种类型.比照 `class template Example <int>;`语句对C++ 11意味着什么?

请参阅此处的文档参考:https://en.cppreference.com/w/cpp/language/class_template#Class_template_instantiation

  • 这是在编译时间!我的意思是**模板Instanciation**. (2认同)