4
回答
C++继承与派生中的虚基类,帮忙看下main函数里的错误
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   
#include <iostream>
using std::cout;
using std::endl;
class vehicle {
private:
int MaxSpeed;
int Weight;
public:
void Run() {cout<<"Running!";}
void Stop() {cout<<"Stop!";}
};
class bicycle:virtual public vehicle {
private:
	int height;
public:
	int showheight() {return height;}
	void setheight(int i) {height=i;}
};
class motorcar:virtual public vehicle {
private:
	int SeatNum;
public:
	int shownum() {return SeatNum;}
};
class motorcycle:public bicycle,public motorcar {
public:
	motorcycle();
	~motorcycle();
};
int main () {
	motorcycle a;
	a.Run();
	a.setheight(5);
	a.showheight;
	return 0;
}
<无标签>
举报
caosz
发帖于5年前 4回/173阅
共有4个答案 最后回答: 5年前

motorcycle的构造函数和析构函数没有定义。

a.showheight后面为什么不加函数调用操作符。

--- 共有 1 条评论 ---
caosz我把那个构造函数和析构函数直接删了,就可以了 5年前 回复

引用来自“canghua”的答案

motorcycle的构造函数和析构函数没有定义。

a.showheight后面为什么不加函数调用操作符。

#include <iostream>
using std::cout;
using std::endl;
class vehicle {
private:
int MaxSpeed;
int Weight;
public:
void Run() {cout<<"Running!";}
void Stop() {cout<<"Stop!";}
};
class bicycle:virtual public vehicle {
private:
	int height;
public:
	int showheight() {return height;}
	void setheight(int i) {height=i;}
};
class motorcar:virtual public vehicle {
private:
	int SeatNum;
public:
	int shownum() {return SeatNum;}
};
class motorcycle:public bicycle,public motorcar {
};
int main () {
	motorcycle a;
	a.Run();
	a.setheight(5);
	a.showheight();
	return 0;
}
顶部