继承、保护.疯狂的编译器
inheritance, protected. Crazy compiler
class Rectangle{
public:
Rectangle(double l=0.0, double w =0.0) : length(l), width(w){}
double getLength() {return lenght;}
double getWidth() { return width;}
virtual double Area() { return length * height;}
protected:
double length, width;
};
class Box : public Rectangle{
public:
// ERROR: compiler error on the next line:
Box(double l, double w, double h) : length(l), width(w), height(h){}
double getHeight() {return height;}
double Volume();
double Area(){
return 6*length * width;
}
private:
double height;
};
在标记行
上出现编译错误Box(double l, double w, double h) : length(l), width(w), height(h){}
编译器报错class Box
没有任何名为width
的字段。有什么问题吗?也许length
在基类中受到保护?
首先你一开始就打错字了。你应该修改:
double getLength() {return lenght;}
:
double getLength() {return length;}
// ^^
其次,在您的Rectangle
类中,您正在使用您未在类中声明的height
标识符:
virtual double Area() { return length * height;}
最后,使用Rectangle
构造函数初始化其成员。所以不是:
Box(double l, double w, double h) : length(l), width(w), height(h){}
使用:
Box(double l, double w, double h) : Rectangle(l, w), height(h){}
如果你修复了每一个错误,你的程序将会编译得很好。
必须调用Rectangle构造函数,而不是Rectangles成员的构造函数!
不能在初始化列表中初始化基类的成员。使用这个
Box(double l, double w, double h) : Rectangle(l,w), height(h){}
相关文章:
- C/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我需要知道编译器如何在cpp中使用析构函数
- 编译器如何区分std::vector的构造函数
- CLANG 编译器 说:变量"PTR"可能未初始化
- 告诉c++编译器该参数没有别名
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么所有C++编译器都会崩溃或挂起此代码
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 继承、保护.疯狂的编译器