关于在c++中实现抽象函数的问题
Question about implementing abstract functions in C++?
我正在学习和测试一段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;
}
相关文章:
- 类C++中的函数问题(LNK2019和LNK1120错误)
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 变分模板递归构造函数问题
- 为每个抽象函数定义主体后,对VTable的不确定引用
- C++函数问题中的数组分配
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
- C++重载构造函数问题
- 二叉搜索树析构函数问题
- C++ vector<pair<int,int>> std::all_of() 函数问题(重定向到头文件)
- 迷宫构造函数问题 [线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)]
- C++函数问题中的 IF 语句
- 函数问题,未定义的引用错误
- C++ 指针无效函数问题
- C++函数问题 = 帮助我理解基本概念
- 由于基于接口的编程,遇到 C++ 虚拟模板函数问题
- C++二进制树递归析构函数问题
- 构造函数问题<无法读取内存>
- 构造函数问题
- 关于在c++中实现抽象函数的问题