代码之家  ›  专栏  ›  技术社区  ›  Schadre

C-plus编码错误

  •  0
  • Schadre  · 技术社区  · 2 年前

    我正在学习C++,我必须完成一个面向对象的程序。我很难理解为什么我会出错。请在解释时尽可能详细地说明,因为这将有助于我自学C++的过程。以下是我的初始代码和错误:

    #include<iostream>
    using namespace std;
    
    class RectangularCube { //Defined the class Rectangular Cube
        private: //Access specifier - Declare two sides as private data field
            string color; //Private data field color that is string type
            double width;
            double height;
    
        public:
        double length; //Declare one side as a public data field
    
        //Contructor with parameteres
        RectangularCube(string newColor, double newWidth, double newHeight, double newLength) {
            color = newColor;
            width = newWidth;
            height = newHeight;
            length = newLength;
        }
    
        RectangularCube() { //No-arguments contructor with all three sides and color set to different values
            color = black;
            width = 5.145;
            height = 4.894;
            length = 10.123;
        }
    
        // Return Volume of this Rectangular Cube
        double getVolume() {
            return length * width * height;
        };
    
        //Return Surface Area of this Rectangular Cube
        double getSurfaceArea() {
            return 2(length * width) + 2(length * height) + 2(width * height)
        };
        
        //get and set functions for the two private sides and color data field
        double getColor();
        double getWidth();
        double getHeight();
        void setColor();
        void setWidth();
        void setHeight();
    
    };
    
    int main() {
        RectangularCube rectangularcube1();
    
        cout << "The Volume of the Rectangular Cube is: "
            << rectangularcube1.getVolume() << endl;
        return 0;
    }
    

    错误

                color = black;
                        ^
    RectangularCube.cpp:35:21: error: called object type 'int' is not a function or function pointer
                return 2(length * width) + 2(length * height) + 2(width * height)
                       ~^
    RectangularCube.cpp:35:41: error: called object type 'int' is not a function or function pointer
                return 2(length * width) + 2(length * height) + 2(width * height)
                                           ~^
    RectangularCube.cpp:35:62: error: called object type 'int' is not a function or function pointer
                return 2(length * width) + 2(length * height) + 2(width * height)
                                                                ~^
    4 errors generated.
    
    2 回复  |  直到 2 年前
        1
  •  3
  •   pm100    2 年前

    通过

    2(length * width)
    

    2*(length * width)
    

    还有这个

     color = black;
    

    应该是的

    color = "black";
    
        2
  •  2
  •   rahul4019    2 年前

    这段代码中总共有三个错误。

    1. 在非参数化构造函数中,您没有将black用作字符串
    2. 创建名为rectangularcube1的新对象时,使用()来调用函数。

    C++:

    #include<iostream>
    using namespace std;
    
    class RectangularCube { //Defined the class Rectangular Cube
    private: //Access specifier - Declare two sides as private data field
        string color; //Private data field color that is string type
        double width;`enter code here`
        double height;
    
    public:
        double length; //Declare one side as a public data field
    
        //Contructor with parameteres
        RectangularCube(string newColor, double newWidth, double newHeight, double newLength) {
            color = newColor;
            width = newWidth;
            height = newHeight;
            length = newLength;
        }
    
        RectangularCube() { //No-arguments contructor with all three sides and color set to different values
            color = "black";  // here you din't write black in string (with double quotes)
            width = 5.145;
            height = 4.894;
            length = 10.123;
        }
    
        // Return Volume of this Rectangular Cube
        double getVolume() {
            return length * width * height;
        };
    
        //Return Surface Area of this Rectangular Cube
        double getSurfaceArea() {
            return 2*(length * width) + 2 * (length * height) + 2 * (width * height);  // you din't use *  operator before brackets
        };
    
        //get and set functions for the two private sides and color data field
        double getColor();
        double getWidth();
        double getHeight();
        void setColor();
        void setWidth();
        void setHeight();
    
    };
    
    int main() {
        RectangularCube rectangularcube1; // you created new object with name rectangluarcube1 but you were using parenthesis like => rectangularcube1();   which is  a function call
    
        cout << "The Volume of the Rectangular Cube is: "
            << rectangularcube1.getVolume() << endl;
        return 0;
    }