My program is supposed to calculate the surface area and volume of a box. I am to have the following functions: setHeight, setWidth, setLength, getVolume, getSurfaceArea.
When I compile my program I get the following error message:
boxMain.cpp: In function ‘int main()’:
boxMain.cpp:21: error: no matching function for call to 'Box::getVolume(double&, double&, double&)’
Box.hpp:20: note: candidates are: double Box::getVolume()
boxMain.cpp:23: error: no matching function for call to ‘Box::getSurfaceArea(double&, double&, double&)’
Box.hpp:21: note: candidates are: double Box::getSurfaceArea()
Based on my searches on this site, most answers seem to suggest that the default constructor doesn't exit or isn't working. I don't know if that is my issue as well, but I wrote the code for my default constructor according to textbook, so I have trouble figuring out what I did wrong.
The missing lines in my code below are just the descriptions and comments that I removed.
Any help would be appreciated! Thank you all so much in advance.
Here is my .hpp file:
#include <iostream>
#ifndef Box_HPP
#define BOX_HPP
class Box {
private:
double h, w, l;
double height, width, length;
public:
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double getVolume();
double getSurfaceArea();
Box();
};
Here is my function .cpp file:
#include <iostream>
#include <iomanip>
#include "Box.hpp"
double height;
double width;
double length;
Box::Box() {
height = 1.0;
width = 1.0;
length = 1.0;
}
void setHeight(double h) {
if(h < 0) {
std::cout << "Error. Height must be positive."
<< std::endl;
}
else {
height = h;
}
}
void setWidth(double w) {
if(w < 0) {
std:: cout << "Error. Width must be positive."
<< std::endl;
}
else {
width = w;
}
}
void setLength(double l) {
if(l < 0) {
std::cout << "Error. Length must be positive."
<< std::endl;
}
else {
length = l;
}
}
double getVolume() {
return (height * width * length);
}
double getSurfaceArea() {
return (2.0 * length * height + 2.0 * width * height);
}
Here is my main .cpp file:
#include <iostream>
#include <fstream>
#include "Box.hpp"
int main() {
Box box1;
double h, w, l;
std::cout << "Enter height" << std::endl;
std::cin >> h;
std::cout << "Enter width" << std::endl;
std::cin >> w;
std::cout << "Enter length" << std::endl;
std::cin >> l;
void setHeight(double h);
void setWidth (double w);
void setLength (double l);
std::cout << "volume: "
<< box1.getVolume(h, w, l) << std::endl;
std::cout << "surface: "
<< box1.getSurfaceArea(h, w, l) << std::endl;
return 0;
}
getSurfaceAreafunction seems wrong to me. Withreturn (2.0 * length * height + 2.0 * width * height);you are only considering the front, back, left, and right faces, but you should also have the top and bottom one. So I think it should bereturn (2.0 * length * height + 2.0 * width * height + 2.0 * length * width);.no matching function for call to 'Box::Box()'. Remember, a constructor has the same name as the class. Here it is clearly indicating another function, so that is where you have to look for errors.