dll导出导入类

在一个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

你可能感兴趣的:(C++,Windows,Tools)