C++继承与派生中的虚基类,帮忙看下main函数里的错误

caosz 发布于 2013/05/26 16:15
阅读 173
收藏 0
#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;
}
加载中
0
c
canghua

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

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

caosz
caosz
我把那个构造函数和析构函数直接删了,就可以了
0
caosz
caosz
34:14: error: statement cannot resolve address of overloaded function

0
x
xuyuheng
Run Stop应该用虚函数
0
caosz
caosz

引用来自“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;
}
返回顶部
顶部