-2

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;
}
Shrikanth N
  • 652
  • 3
  • 17
lauren
  • 65
  • 1
  • 1
  • 11
  • Please post a [mcve]. – melpomene Oct 23 '18 at 01:55
  • `calcVolume()` accepts arguments. You are trying to call it without passing parameters – GBlodgett Oct 23 '18 at 01:55
  • @GBlodgett right okay so when I try box1.calcVolume(double h, double w, double l); it gives me the same error. Is this what you are referring to? – lauren Oct 23 '18 at 01:56
  • 2
    Look at `box2.setWidth(22.3);`, you are calling a member function that takes one argument and you are passing a value `22.3`. Your other functions are written to also take arguments but you are **not** passing anything. That's an error. – Blastfurnace Oct 23 '18 at 02:00
  • That's a syntax error. It should give you a different error message. – melpomene Oct 23 '18 at 02:00
  • How many parameters does `calcSurfaceArea(...)` ask for and how many are you passing to it? – Galik Oct 23 '18 at 02:28

3 Answers3

1

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;
}
Steve
  • 6,334
  • 4
  • 39
  • 67
  • I tried that I'm still getting an error. Do you think I need to set w = width etc instead of width = w in my setter functions? – lauren Oct 23 '18 at 02:06
  • 1
    What is the error you are getting? Did you remember to adjust `calcVolume()'s` declaration in the class declaration to remove the arguments? The same issue applies to the `calcSurfaceArea()` method, too – Remy Lebeau Oct 23 '18 at 02:08
  • @RemyLebeau I am still getting too few arguments in function call – lauren Oct 23 '18 at 02:09
  • @lauren You didn't answer my other question. Steve's answer shows you to remove the arguments from the *implementation* of `calcVolume()`, but did you also remove them from the *declaration* of `calcVolume()` to match? – Remy Lebeau Oct 23 '18 at 02:10
  • Everywhere there is a `calcVolume(` somewhere in your program (*everywhere*), it should be immediately followed by `)`, with nothing in between. – Steve Oct 23 '18 at 02:10
  • Also, why would you use `w = width` in `setWidth()`? The value given to that function is *the value to store*, and `width = w` means "put the value that is in `w` into `width`". You *must* understand this concept as it's about the simplest there is. – Steve Oct 23 '18 at 02:12
  • @RemyLebeau oh sorry for some reason I didn't see the rest of the quesiton. If you are referring to the function definition yes I did change it to return height * width * length using those variable names. I changed it for both calcVolume and calcSurfaceArea() – lauren Oct 23 '18 at 02:12
  • @Steve Ok so in my cpp file should I not have calcVolume(double h, double w, double l) for example? Should I just have calcVolume() – lauren Oct 23 '18 at 02:13
  • "I did change it to return height * width * length" You *also* need to remove the parameters from the function prototype: `double Box::calcVolume(double h, double w, double l)` becomes `double Box::calcVolume()`, for example. – Steve Oct 23 '18 at 02:13
  • @lauren that is not what I am referring to. The signature of the *implementation* of the method in the CPP file must match the signature of the *declaration* of the method in the H file. It sounds like you have NOT updated the H file. Change `double calcVolume(double h, double w, double l);` in the H file to `double calcVolume();` and change `double Box::calcVolume(double h, double w, double l)` in the CPP file to `double Box::calcVolume()` Do you see now? – Remy Lebeau Oct 23 '18 at 02:13
  • @lauren Yes, because you *don't need to pass any data to that method*. The object instance of the `Box` class already knows its own dimensions, that's kind of the point of an object. – Steve Oct 23 '18 at 02:14
  • @RemyLebeau I did remove the arguments in function and the error went away. However I am confused as to why I shouldn't put arguments in my functions. – lauren Oct 23 '18 at 02:15
  • @lauren you have already been told why. Because THEY ARE NOT NEEDED, because a `Box` object has internal variables that serve the same purpose as the arguments that are being removed. The caller DOES NOT need to pass in the arguments AGAIN, because they were already passed in EARLIER to the `set...()` methods, which REMEMBERED the values so the `Box` methods can then use them. – Remy Lebeau Oct 23 '18 at 02:16
  • @lauren It's not a matter of not putting arguments in your function , it's a matter if the function takes arguments to begin with. – GKE Oct 23 '18 at 02:16
  • @RemyLeau Ok I think I understand this better now. I'm gonna get reread some material to see if I can understand it better. I appreciate your help! – lauren Oct 23 '18 at 02:18
0

You have the code wrong. The dimensions of the box are known during the creation of box object. The length, width and height are already available - updated in the member variables.

Functions calcVolume and calcSurfaceArea should not take arguments but return the computed value. Modified code below:

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

double Box::calcSurfaceArea()
{
   return 2*((height*width) + (height*length) + (length*width));
}

Also, remember to modify the .hpp file with the declarations corresponding to the code above.

Declaration in the .hpp file should be

double calcVolume();
double calcSurfaceArea();
Shrikanth N
  • 652
  • 3
  • 17
0

I have solved the problem. I removed the arguments in calcVolume and calcSurfaceArea everywhere in my code and it resolved the error.

lauren
  • 65
  • 1
  • 1
  • 11
  • This is not a forum, but has a different format. You should click the check-mark next to the best answer, then I recommend checking out the [Help Center](https://stackoverflow.com/help). – Steve Oct 23 '18 at 02:46
  • @Steve I am new. I did put a check-mark next to the best answer. Thank you – lauren Oct 23 '18 at 02:51