gcc error: 'class A' has no member named 'b':误用多态导致的错误
来源:互联网 发布:excel多表数据合计 编辑:程序博客网 时间:2024/05/16 09:27
我读本科时候所用的程序设计教材上有这么一句话:“指向基类对象的指针可以指向该基类的公有派生类对象(注意反过来不成立),这是运行时多态性的基础。指向基类对象的指针指向派生类对象后,可以访问派生类对象中由基类继承下来的成员,但不能访问那些派生类对象自己增加的成员。”
为了更好地说明问题,简单写几行代码。我犯的错误就在于忘记了红字标注的内容。首先是a.h:
class A{ public:int a;A();virtual ~A();};
接下来是a.cpp:
#include "a.h"A::A(){ a = 2;}A::~A(){}
然后是b.h:
#include "a.h"class B: public A{ public:int b;B();~B();};
最后是b.cpp:
#include "b.h"B::B(){ a = 0; b = 1;}B::~B(){}
写一个main.cpp来测试:
#include <iostream>#include "a.h"#include "b.h"using namespace std;int main(){ A *p1, *p2; p1 = new A; p2 = new B; cout << "A: a = " << p1 -> a << endl; cout << "B: a = " << p2 -> a << endl; cout << "B: b = " << p2 -> b << endl; delete p1; delete p2; return 0;}
下面用gcc进行编译: g++ main.cpp a.cpp b.cpp -o main.exe
结果报错,如标题所示。什么原因?读过前文,大家都已经清楚了,就不多说了。
- gcc error: 'class A' has no member named 'b':误用多态导致的错误
- error: ‘class QApplication’ has no member named ‘setMainWidget’
- 错误:'class QApplication' has no member named 'setMainwidget'
- 'class QApplication' has no member named 'setMainWidget'
- error: 'struct proc_dir_entry' has no member named 'owner'的解决方法
- error: 'struct proc_dir_entry' has no member named 'owner'的解决方法
- Qt: error: 'class QString' has no member named 'toAscii'(class QChar)
- 虚拟机安装vmtools 共享文件夹失败 错误:error: ‘struct dentry’ has no member named ‘d_alias’
- error: ‘struct device’ has no member named ‘bus_id’
- error: ‘struct device’ has no member named ‘bus_id’
- error: 'struct passwd' has no member named 'pw_comment'
- error: ‘struct hostent’ has no member named ‘h_addr’
- error: ‘struct dentry’ has no member named ‘d_count’
- error: ‘struct hostent’ has no member named ‘h_addr’
- truct in6_addr has no member named in6_u
- Linux-world-2012-January->13(error: 'S3C24X0_GPIO' has no member named 'GPACON')
- php5.4.26 安装出现 error: ‘struct gdIOCtx’ has no member named ‘data’
- VMware 共享文件夹 error: ‘struct dentry’ has no member named ‘d_alias’
- 面向对象设计的优点
- FFF Warm Up Contest 13 - NWERC 2011 [iSea]
- C 语言中的控制结构
- hibernate的延迟加载及其与session关闭的矛盾
- “应用程序配置不正确,程序无法启动” 解决办法(vc2008 sp1)
- gcc error: 'class A' has no member named 'b':误用多态导致的错误
- Sql Create Function简单例子
- 东子破解的第一天
- modbus16单独函数类,DSP串口通信,F28335
- javascript静态局部变量
- PHP Smarty环境配置
- DirectX组件之---开始DirectShow旅程
- MFC记录式文件的读写
- FS_11C14平台ZigBee点对点通信