标题:程序编译错误 求指教
只看楼主
糊涂无罪
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:161
注 册:2012-5-28
结帖率:77.78%
已结贴  问题点数:20 回复次数:8 
程序编译错误 求指教
#include <iostream>
using namespace std;
class vehicle{
public:
    void run (){};
    void stop (){};
    vehicle(double ms,double w){
        maxspeed=ms;
        weight=w;
        cout<<"constring vehicle in called"<<endl;}
private:
    double maxspeed;
    double weight;
};
class bicycle:public vehicle{
public:
    bicycle(double h,double ms,double w):vehicle(ms,w){
        hight=h;
        cout<<"constring bicycle is called"<<endl;}
private:
    double hight;
};
class motorcar:public vehicle{
public:
    motorcar(double n,double ms, double w):vehicle(ms,w){
        seatnum=n;
        cout<<"constring motorcar is called"<<endl;}
private:
    double seatnum;
};
class motorcycle:public bicycle,public motorcar{
public:
    motorbicycle(double n,double h,double ms,double w):bicycle(double h,double ms,double w),
        motorcar(double n,double ms,double w)
    {cout<<"constring of motorbicycle is called"<<endl;}
};
int main(){
    motorbicycle m1(1.0,2.0,3.0,4.0);
    return 0;
}
搜索更多相关主题的帖子: double void private include public 
2012-05-28 17:53
lknight
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:187
注 册:2008-9-17
得分:15 
#include <iostream>
using namespace std;
class vehicle{
public:
    void run (){};
    void stop (){};
    vehicle(double ms,double w){
        maxspeed=ms;
        weight=w;
        cout<<"constring vehicle in called"<<endl;}
private:
    double maxspeed;
    double weight;
};
class bicycle:public vehicle{
public:
    bicycle(double h,double ms,double w):vehicle(ms,w){
        hight=h;
        cout<<"constring bicycle is called"<<endl;}
private:
    double hight;
};
class motorcar:public vehicle{
public:
    motorcar(double n,double ms, double w):vehicle(ms,w){
        seatnum=n;
        cout<<"constring motorcar is called"<<endl;}
private:
    double seatnum;
};
class motorcycle:public bicycle,public motorcar{
public:
    motorbicycle(double n,double h,double ms,double w):bicycle(double h,double ms,double w),
        motorcar(double n,double ms,double w)//这句首先这个构造函数名字和类名字不一样,再者父类初始化列表按你的做法应该这写:bicycle(h,ms,w),motorcar(n,ms,w)
    {cout<<"constring of motorbicycle is called"<<endl;}
};
int main(){
    motorbicycle m1(1.0,2.0,3.0,4.0);//类的名字打错了
    return 0;
}
2012-05-28 21:17
sunny孙
Rank: 2
等 级:论坛游民
帖 子:18
专家分:37
注 册:2011-11-4
得分:2 
注意虚基类的应用···
2012-05-28 21:50
丨丨丨
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2012-5-21
得分:3 
二楼正确,这种错误可不能常犯哦
2012-05-29 13:04
nongzhaobo
Rank: 3Rank: 3
来 自:广西南宁
等 级:论坛游侠
帖 子:59
专家分:155
注 册:2012-4-8
得分:0 
看不懂了....
2012-05-29 16:54
糊涂无罪
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:161
注 册:2012-5-28
得分:0 
受教了,谢谢各位!问题解决了,二楼辛苦了。有劳各位了,以后多指教。
2012-05-30 09:57
糊涂无罪
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:161
注 册:2012-5-28
得分:0 
麻烦再问一下,使用虚基类有什么好处呢?求指教。谢谢!
2012-05-30 09:59
lknight
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:187
注 册:2008-9-17
得分:0 
使用虚基类在实例化的时候可以避免歧义,比如说有基类A,另有类B和C都继承了A,然后再有类D继承了B和C,这时候如果没有使用虚基类,D在访问从A类继承的方法和属性的时候就有可能出现歧义,因为B和C在实例化的时候内存中都有一份A的实例,编译器不知道是访问的方式,因为D->B->A和D->C->A都可以;如果使用虚基类B和C中存在的就不是A的实例了,而是存在一个指向A实例的指针,D访问从A类继承的方法和属性时就没有歧义,但是这也并不能完全消除歧义,需要设计类时合理安排。
2012-05-30 20:42
糊涂无罪
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:161
注 册:2012-5-28
得分:0 
回复 8楼 lknight
问题解决,谢谢解惑!
2012-05-31 18:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-368191-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.100909 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved