“错误:未解析的外部符号”每当我使用纯虚函数 [英] "Error: Unresolved external symbol" whenever I use a pure virtual function

查看:120
本文介绍了“错误:未解析的外部符号”每当我使用纯虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我做了一些令人难以置信的蠢事,但我根本无法弄清楚我的代码有什么问题。我甚至做了一个超级简化版本的代码,错误仍然发生:

I feel like I'm doing something incredibly stupid, but I simply can't figure out what's wrong with my code. I even made a super simplified version of the code and the error still occurs:

#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);

}

任何人都可以发现我肯定是最愚蠢的错误的所有时间?

Can anyone spot what I'm sure is the silliest error of all time?

以下是确切的错误讯息:

Here's the exact error message:

1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall c1::add(int,int)" (?add@c1@@UAEHHH@Z)


推荐答案

virtual int add(int a, int b);

这不是纯虚函数的声明。它只是一个虚函数的声明。它缺少一个定义,这就是为什么你得到的错误。

This is not a declaration of a pure virtual function. It's just a declaration of a virtual function. It lacks a definition, which is why you get the error.

virtual int add(int a, int b) = 0;

这是一个纯虚函数的声明。它不需要定义,这就是为什么不会得到错误。

This is a declaration of a pure virtual function. It does not require a definition, which is why won't get the error.

这篇关于“错误:未解析的外部符号”每当我使用纯虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆