xcd*_*n05 3 c++ inheritance virtual-functions class
我觉得我做的事情非常愚蠢,但我根本无法弄清楚我的代码有什么问题.我甚至制作了代码的超级简化版本,但仍然出现错误:
#include <iostream>
using namespace std;
class c1{
public:
c1(){}
~c1(){}
virtual int add(int a, int b);
private:
protected:
};
class c2 : c1{
public:
c2(){}
~c2(){}
int add(int a, int b){
return a+b;
}
};
int main(){
c2 c;
c.add(5,6);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以发现我确信有史以来最愚蠢的错误吗?
这是确切的错误消息:
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall c1::add(int,int)" (?add@c1@@UAEHHH@Z)
Run Code Online (Sandbox Code Playgroud)
R. *_*des 12
virtual int add(int a, int b);
Run Code Online (Sandbox Code Playgroud)
这不是纯虚函数的声明.它只是一个虚函数的声明.它没有定义,这就是你得到错误的原因.
virtual int add(int a, int b) = 0;
Run Code Online (Sandbox Code Playgroud)
这是纯虚函数的声明.它不需要定义,这就是为什么不会得到错误.