首页 > 解决方案 > 由于动态链接库中的虚函数导致编译错误

问题描述

在 MSVC dll 项目中,我尝试创建一个包含基类的 dll

//header file
class MATHLIBRARY_API Shape {
protected:
    int width, height;
public:
    Shape(int, int);
    int area(void) { return -1; };
};

它已成功编译,但是在向函数添加虚拟说明符时

class MATHLIBRARY_API Shape {
protected:
     int width, height;
public:
     Shape(int, int);
     virtual int area(void) { return -1; };
};

编译器显示错误消息

Error   LNK2019 unresolved external symbol `__declspec(dllimport) const Shape::`vftable'" (__imp_??_7Shape@@6B@) referenced in function "public: __thiscall Shape::Shape(int,int)" (??0Shape@@QAE@HH@Z) Dll3    c:\Users\langj\source\repos\Dll3\Dll3\Dll3.obj  1   

问题出在哪里?

标签: c++visual-studiovisual-c++dlllnk2019

解决方案


推荐阅读