dll导出导入类

15 篇文章 0 订阅
13 篇文章 0 订阅

在一个DLL工程中写入一个类并导出,如下:

class __declspec(dllexport) Point 
{ 
public: 
Point(int _x=0, int _y=0) : x(_x), y(_y) {} 
private: 
int x, y; 
};

将编译后的lib和dll文件拷入另一个工程,采用静态链接(指定lib库):

类声明如下:

class __declspec(dllimport) Point;

main函数中构造一个对象:

Point p(5, 3);
提示出错:

error C2079: ‘p’ uses undefined class ‘Point’

用此方法导出导入函数都正确运行,但为什么不能导入一个类呢???????

另外,若用动态连接LoadLibrary后用GetProcAddress可以获取dll中函数地址,从而使用之。但如何从动态加载的dll中获取类的信息进而用来构造对象?????

2009年1月25日 8:04
class __declspec(dllimport) Point;这句话不能这样写

这样写就没问题了.

class __declspec(dllimport) Point
{
public:
 Point(int _x=0, int _y=0) ;
};

导出的定义也不能这样写

class __declspec(dllexport) Point
{
public:
 Point(int _x=0, int _y=0) : x(_x), y(_y) {}
private:
 int x, y;
};

析构函数在导出类时必须显式

class __declspec(dllexport) Point
{
public:
 Point(int _x=0, int _y=0) : x(_x), y(_y) {}

 ~Point() {/*NULL*/}
private:
 int x, y;
};

否则链接会出错.
2009年1月27日 13:56

没有包含头文件。
2009年1月25日 14:54

参考网址 https://social.msdn.microsoft.com/Forums/zh-CN/063f27f7-53aa-408c-bc29-dee98c2d5ab7/dll?forum=visualcpluszhchs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值