0

Please see the error and help me. Its showing error. I just can't understand where is the problem?? I have resolved all the errors that could be solved but still I am stuck. I know this is a vey simple question and its very frustrating.

The question is: Create a class calculate that uses two separate functions:-a function to multiply two float numbers and another to divide two float numbers using the concept of inline function. Also, create a method to print the result on the screen.

#include <iostream>
using namespace std;

class calculate
{
    float a,b;

    public:
    float multiply(float a, float b);
    float divide(float a, float b);
};

inline float calculate :: multiply(float a, float b)
{
     return a*b;
}

inline float calculate :: divide(float a, float b)
{
    return a/b;
}

int main()
{
    float a,b;
    cout<<"Enter two float numbers: ";
    cin>>a>>b;

    calculate obj (a,b);
    cout<<"Multiplication: "<<obj.multiply(a,b)<<endl;
    cout<<"Division: "<<obj.divide(a,b)<<endl;

    return 0;
}
  • 2
    You have to describe the problem, show the error etc, not expect us to read through the entire problem to find it. Error messages are great like that -- they tell you exactly where to look and what the problem is. Saves a lot of time. – paddy Mar 29 '22 at 08:05
  • 1
    You do not check `std::cin` after input – get used to always do to catch invalid input! – Aconcagua Mar 29 '22 at 08:11
  • Off-topic: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Mar 29 '22 at 08:12
  • 2
    Probably unrelated to whatever your error is, but your class does not need those member variables `a` and `b`, you pass the values as parameters anyway. There's no need to have a class at all, a namespace would suffice. – Aconcagua Mar 29 '22 at 08:14

1 Answers1

2

Compiling your program yields the compiler error:

error: no matching function for call to 'calculate::calculate(float&, float&)'
   29 |     calculate obj (a,b);

This is telling you that on the line where you construct a calculate object named obj by invoking a constructor that takes two float arguments, no such constructor in the class calculate exists.

Looking at the class, you did not define any such constructor. This is easily fixed:

class calculate
{
    float a, b;

public:
    calculate(float a, float b);
    float multiply(float a, float b);
    float divide(float a, float b);
};

calculate::calculate(float a, float b)
    : a(a)
    , b(b)
{
}

Now your class can be constructed as you wanted:

calculate obj(a, b);
paddy
  • 60,864
  • 6
  • 61
  • 103