Search code examples
c++functionoopobjectfunction-call

Too few arguments in function call C++


Hello I am a student learning c++ and I am just starting to learn OOP. The problem is in my MAIN however I am showing all of my files in case it is from another file.

I have written my hpp and cpp file and now I am just working on my main for testing. The class is called Box and when I create an object box1 or box 2 and attempt to access my functions it says there are two few arguments. It says this regardless of whether I put box1.calcVolume(double h, double w, double l) or box1.calcVolume();

So the issue is on the line(s) that say:

double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();

If anyone can spot something that I missing our may not understand please let me know.

This is the header file:

#pragma once

#include <iostream>
#ifndef BOX_HPP
#define BOX_HPP

class Box
{
private:
    double height;
    double width;
    double length;

public:
    void setHeight(double h);
    void setWidth(double w);
    void setLength(double l);
    double calcVolume(double h, double w, double l);
    double calcSurfaceArea(double h, double w, double l);
    Box();
    Box(double height, double width, double length);    
}; 
#endif

This is the CPP file

#include <iostream>
#include "Box.hpp"

Box::Box()
{
    setHeight(1);
    setWidth(1);
    setLength(1);
}

Box::Box(double h, double w, double l) 
{
    setHeight(h);
    setWidth(w);
    setLength(l);
}

void Box::setHeight(double h)
{
    height = h;
}

void Box::setWidth(double w)
{
    width = w;
}

void Box::setLength(double l)
{
    length = l;
}

double Box::calcVolume(double h, double w, double l)
{
    double volume;
    volume = h * w * l;
    return volume;
}

double Box::calcSurfaceArea(double h, double w, double l)
{
    double surfaceArea;
    surfaceArea = 2 * (h*w) + 2 * (h*l) + 2 * (l*w);
    return surfaceArea;
}

my BoxMain file:

#include <iostream> 
#include "Box.hpp"

using std::cout;
using std::cin;
using std::endl;

int main()
{
    Box box1(1.1, 2.4, 3.8);
    Box box2;
    box2.setHeight(12);
    box2.setWidth(22.3);
    box2.setLength(2.3);

    double volume2 = box2.calcVolume();

    double volume1 = box1.calcVolume();
    double surfaceArea1 = box1.calcSurfaceArea();
    cout << box1.calcVolume(); << endl;  //testing different methods

    return 0;
}

Solution

  • Your method takes three parameters:

    double Box::calcVolume(double h, double w, double l)
    {
        double volume;
        volume = h * w * l;
        return volume;
    }
    

    So, you would call it like so:

    Box b;
    double volume = b.calcVolume(1, 2, 3);
    

    But that's not quite right. An instance of Box knows how big it is, because you pass a size to the constructor, which stores the sizes in the fields width, height, and length. You probably want something like this:

    double Box::calcVolume()
    {
        volume = height * width * length;
        return volume;
    }