'I just can't get it. What is wrong in this?
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;
}
Solution 1:[1]
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);
Solution 2:[2]
#include <iostream>
using namespace std;
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)
{
}
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;
}
Credit:-Paddy(https://stackoverflow.com/users/1553090/paddy)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | paddy |
Solution 2 | shubham00000 |