I am learning C++ and I have to complete an Object Oriented Program. I am struggling to understand why I am getting an error. Please give as much as detail as possible when explaining because it would be helpful in the journey of teaching myself C++. Below is my initial code and the error:
#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;
}
The Error
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.
>Solution :
By
2(length * width)
I assume you really mean
2*(length * width)
and this
color = black;
should probably be
color = "black";