标题:弄成虚基类后就出现好多错误= =读不懂错误原因,怎么读懂
只看楼主
Orphan
Rank: 1
来 自:僵尸大
等 级:新手上路
帖 子:9
专家分:0
注 册:2015-5-21
结帖率:0
已结贴  问题点数:10 回复次数:2 
弄成虚基类后就出现好多错误= =读不懂错误原因,怎么读懂
#include<iostream>
using namespace std;
class vehicle
{protected:
   int wheel;
   int weight;
public:
   vehicle(int i,int j)
   {
     wheel=i;
     weight=j;
   }
   void display()
   {
     cout<<wheel<<endl;
     cout<<weight<<endl;
   }

};
class car:virtual public vehicle
{protected:
   int passengers;
public:
   car(int wh,int we,int p):vehicle(wh,we)
   {
     passengers=p;
   }
   void display()
   {
     cout<<wheel<<endl;
     cout<<weight<<endl;
     cout<<passengers<<endl;
   }

};
class truck:virtual public vehicle
{protected:
   double payload;
 public:
   truck(int wh,int we,double pay):vehicle(wh,we)
   {
     payload=pay;
   }
   void display()
   {
     vehicle::display();
     cout<<payload<<endl;
   }
      
};
class car_truck:public vehicle,public car,public truck
{protected:
   int flag;
public:
   car_truck(wh1,we1,p1,f):vehicle(wh1,we1,p1,f):car(wh1,we1,p1,f):truck(wh1,we1,p1,f)
   {
     flag=f;
   }
   void display()
   {
     if(flag==0)
     {
       cout<<"this is a car"<<endl;
       car::display();
     }
     else
     {
       cout<<"this is a truck"<<endl;
       truck::display();
     }
   }
      
};
int main()
{
   vehicle v(3,4);
   v.display();
   car c(3,4,5);
   c.display();
   truck t(3,4,8.4);
   t.display();
   car_truck ct(3,4,5,0);
   ct.display();
   return 0;

}
//Compiling...
.cpp
E:\dingzhw\dze\.cpp(51) : error C2584: 'car_truck' : direct base 'vehicle' is inaccessible; already a base of 'car'
        E:\dingzhw\dze\.cpp(4) : see declaration of 'vehicle'
E:\dingzhw\dze\.cpp(52) : error C2584: 'car_truck' : direct base 'vehicle' is inaccessible; already a base of 'truck'
        E:\dingzhw\dze\.cpp(4) : see declaration of 'vehicle'
E:\dingzhw\dze\.cpp(55) : error C2629: unexpected 'class car_truck ('
E:\dingzhw\dze\.cpp(55) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\dingzhw\dze\.cpp(82) : error C2660: 'car_truck::car_truck' : function does not take 4 parameters
执行 cl.exe 时出错.

dze.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: display include public 
2015-06-10 17:10
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
得分:5 
这是多重继承的问题,你的car,trunk已经基础vehicale当你的car_trunk继承vehicle,car,trunk时其实继承了三个car

一片落叶掉进了回忆的流年。
2015-06-10 18:54
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
class car_truck:virtual public vehicle,public car,public truck
{protected:
   int flag;
public:
   car_truck(int wheel,int weight,int passengers,double payload,int flag_):vehicle(wheel,weight),car(wheel,weight,passengers),truck(wheel,weight,payload)
   {
     flag=flag_;
   }
2015-06-11 08:31



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




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

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