【问题标题】:C++ invalid operands of types 'double' and 'const char [5]' to binary 'operator+''double' 和 'const char [5]' 类型的 C++ 无效操作数到二进制 'operator+'
【发布时间】:2018-04-18 14:50:18
【问题描述】:

我是 C++ 新手,我已经学习了大约 4 周,现在我开始尝试编写自己的东西,比如我正在做的事情:

#include <iostream>
#include <cmath>
using namespace std;

class abcformule{
    public:
        int discriminant(double a, double b, double c){
            return pow(b, 2) - 4 * a *c;
        }

        void answer(double a2, double b2, double c2){
            cout << (-b2 + discriminant(a2, b2, c2)) / (2 * a2) + "and " + (-b2 - discriminant(a2, b2, c2)) / (2 * a2);
        }
};

int main() {
    abcformule abc;
    abc.answer(5, -2, -7);
    return 0;
}

它还没有完成,但正如您可能猜到的那样,它应该使用 abc 公式来求解 x 的二次公式。 (我才14岁,不想做作业lol)。

所以,它给了我这个错误:

12  58  D:\c++ dev-c\abc.cpp    [Error] invalid operands of types    'double' and 'const char [5]' to binary 'operator+'

这是什么意思,我该怎么做才能解决它?

【问题讨论】:

    标签: c++ operands


    【解决方案1】:

    问题是您不能像在打印语句中那样使用“+”运算符。请改用“

    cout << (-b2 + discriminant(a2, b2, c2)) / (2 * a2) << " and " << (-b2 - discriminant(a2, b2, c2)) / (2 * a2) << "\n";
    

    【讨论】:

    • 祝你好运!那天,我在计算器上写​​了与我的第一个基本程序相同的问题。练习肯定会变得更容易!
    【解决方案2】:
     + "and " + 
    

    在行中

     cout << (-b2 + discriminant(a2, b2, c2)) / (2 * a2) + "and " + (-b2 - discriminant(a2, b2, c2)) / (2 * a2);
    

    不能作为 + 之前的部分和第二个 + 之后的部分是要添加到字符串中的双精度值。 使用“ 直接使用 cout 打印这些双精度值

    【讨论】:

    • 当您可以单独打印语句时,我不知道将双精度转换为字符串是否真的有意义。
    • 你说得对,我真的不知道如何“拼写”它,但“
    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多