相关疑难解决方法(0)

C++从DLL实例化模板类

我试图制作一个包含以下内容的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 …
Run Code Online (Sandbox Code Playgroud)

c++ dll templates function-pointers

2
推荐指数
1
解决办法
8945
查看次数

标签 统计

c++ ×1

dll ×1

function-pointers ×1

templates ×1