我只能得到它。这有什么问题?

发布于 2025-01-17 12:48:51 字数 797 浏览 2 评论 0原文

请查看错误并帮助我。它显示错误。我就是不明白问题出在哪里??我已经解决了所有可以解决的错误,但我仍然陷入困境。我知道这是一个非常简单的问题,而且非常令人沮丧。

问题是: 创建一个使用两个单独函数的计算类:一个函数用于将两个浮点数相乘,另一个函数使用内联函数的概念来除两个浮点数。另外,创建一个在屏幕上打印结果的方法。

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2025-01-24 12:48:51

编译程序会产生编译器错误:

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

这告诉您,在构造计算对象的线路上,通过调用一个构造函数,该构造函数获取两个float参数,没有这样类中的构造函数计算存在。

看课,您没有定义任何此类构造函数。这很容易解决:

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)
{
}

现在您的班级可以按照您的意愿构建:

calculate obj(a, b);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文