64,282
社区成员
发帖
与我相关
我的任务
分享
//书上说可以同过基类的指针调用 ,编译通过,但是为什么执行的时候会出错了
// 望大家指点
#include<iostream>
using namespace std;
class Base
{
public:
virtual int fcn();
};
class D1:public Base
{
public:
int fcn(int);
};
class D2:public D1
{
public:
int fcn(int);
int fcn();
};
int main()
{
Base bobj;
D1 d1obj;
D2 d2obj;
Base *bp1=&bobj,*bp2=&d1obj,*bp3=&d2obj;
bp1->fcn();
bp2->fcn();
bp3->fcn();
return 0;
}
///////////////////////////////////////////////////
------ 已启动生成: 项目: tre, 配置: Debug Win32 ------
正在链接...
fwe.obj : error LNK2001: 无法解析的外部符号 "public: virtual int __thiscall Base::fcn(void)" (?fcn@Base@@UAEHXZ)
fwe.obj : error LNK2001: 无法解析的外部符号 "public: virtual int __thiscall D2::fcn(void)" (?fcn@D2@@UAEHXZ)
E:\C++练习题\tre\Debug\tre.exe : fatal error LNK1120: 2 个无法解析的外部命令
生成日志保存在“file://e:\C++练习题\tre\Debug\BuildLog.htm”中
tre - 3 错误,0 警告
---------------------- 完成 ---------------------
生成: 0 已成功, 1 已失败, 0 已跳过
//我实现了一下虚函数但是,却没有显示任何结果
//不明白,还请指点
#include<iostream>
using namespace std;
class Base
{
public:
virtual int fcn()
{return 8*8;}
};
class D1:public Base
{
public:
int fcn(int);
};
class D2:public D1
{
public:
int fcn(int);
int fcn()
{return 9*9;}
};
int main()
{
Base bobj;
D1 d1obj;
D2 d2obj;
Base *bp1=&bobj,*bp2=&d1obj,*bp3=&d2obj;
bp1->fcn();
bp2->fcn();
bp3->fcn();
system("pause");
return 0;
}
//C++primer上说,虽然没定义重载虚函数
//但是可以通过基类指针调用
//这是书上的列子,我没有看懂,希望
//楼下的解答