Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 继承,保护。疯狂编译器_C++ - Fatal编程技术网

C++ 继承,保护。疯狂编译器

C++ 继承,保护。疯狂编译器,c++,C++,我在标记行上得到一个编译错误 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:

我在标记行上得到一个编译错误

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;
};

编译器抱怨
类框
没有任何名为
宽度
的字段。有什么问题吗?也许
length
在基类中受保护?

您必须调用矩形构造函数,而不是矩形成员的构造函数

首先,你一开始就有打字错误。您应该更改:

Box(double l, double w, double h) : length(l), width(w), height(h){}
与:


其次,在
矩形
类中,您正在使用未在类中声明的
高度
标识符:

double getLength() {return length;}
//                             ^^

最后,使用
矩形
构造函数初始化其成员。因此,不是:

virtual double Area() { return length * height;}
使用:


如果您修复了每个错误,您的程序将。

您无法在初始化列表中初始化基类的成员。用这个

Box(double l, double w, double h) : Rectangle(l, w), height(h){}

总工程师?你得到了什么编译器错误?毕竟,我写道:类框没有任何字段名。请说出确切的错误。该死的那些疯狂的编译器!我很好奇你为什么要把你的错误归咎于编译器。
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){}
Box(double l, double w, double h) : Rectangle(l,w), height(h){}