关于在c++中实现抽象函数的问题

Question about implementing abstract functions in C++?

本文关键字:抽象函数 问题 实现 c++      更新时间:2023-10-16

我正在学习和测试一段c++代码,如下所示:

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cstring>
class Shape {
public:
    Shape() {};
    ~Shape() {};
    virtual void display() const = 0;
    virtual double volume() const = 0;
};
class Square : public Shape {
public:
    Square() {};
    ~Square() {};
    void display() const;
    double volume() const;
};
void Square::display() const {
    cout << "Square!!!!!!!!!!!!!!" << endl;
}
double Square::volume() const {
    cout << "Square Volume........." << endl;
    return 0.0;
}
int _tmain(int argc, _TCHAR* argv[])
{
    Shape *s;
    s = new Square; // error here
    (*s).display();
    return 0;
}

上面的代码编译不成功。它会产生:"致命错误LNK1120: 1 unresolved externals"。有人能帮我一下吗?我使用MS VS c++ 2005。由于

以上代码可以在VS 2010和Ideone上正常编译和运行。

检查这个

你在上面代码片段中实现抽象函数的方式没有任何问题。

我很确定你的问题是你的主要声明。

如果您将其更改为标准的主定义,我相信您的链接问题将得到解决。

 int main()
 {
   Shape *s = new Square(); // error here
   s->display();
   return 0;
 }