C++ 错误 : invalid Operands of types 'double' and <unresolved overloaded function type' to binary 'operator'

标签 c++ converters

我是编程初学者,目前正在尝试制作一个从公斤到磅的转换程序,反之亦然。我不擅长阅读错误代码,所以有人可以告诉我,我做错了什么。

#include <iostream>

using namespace std;

int main()
{

    char response;

    double  Kilo_const = 0.45, Pound_const = 2.20, Kilo_output, Pound_output;

    double Kilo_input, Pound_input;

    cout << "Choose the input unit \nk = kilo and p = pound" << endl;

    cin >> response;

    if (response == 'k'){
        cin >> Kilo_input;
        cout << Pound_output = Kilo_input * Pound_const << endl;
    }
    else (response == 'p'){
        cin >> Pound_input;
        cout << Kilo_output = Pound_input * Kilo_const << endl;
    }
    return 0;
}

最佳答案

你的问题是第 21 行:

cout << Pound_output = Kilo_input * Pound_const << endl;

您在这里尝试做的是为 Pound_output 赋值,然后将其传递给 cout,这是行不通的。

您可以这样做(注意括号!感谢 Alan):

cout << (Pound_output = Kilo_input * Pound_const) << endl;

Pound_output = Kilo_input * Pound_const;
cout << Pound_output << endl;

首先会进行转换并打印出来,或者你也可以这样做

cout << Kilo_input * Pound_const << endl;

它不会使用变量来存储结果,而是立即打印它。

这同样适用于您的第二次转化。

此外,您的 if 子句还有第二个错误。语法是

if (...) { } else if (...) { }

你在哪里忘记了第二个 if。如果不存在,则 else 标记没有条件并且将在第一条语句失败时执行。注意区别:

if (a == 1) { cout << "Execute on a = 1"; } else { cout << "Execute on a != 1"; }

if (a == 1) { cout << "Execute on a = 1"; } else if (a == 2) { cout << "Execute on a != 1 and a = 2"; }

关于C++ 错误 : invalid Operands of types 'double' and <unresolved overloaded function type' to binary 'operator' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424467/

相关文章:

c++ - Arduino 上用户控制​​的 SD 卡目录遍历

c++ - 如何在 C 套接字上将超时设置为 "connect()"函数?

c++ - 为什么 std::string 不定义乘法或文字?

c# - 在 WPF 中使用值转换器而不必先将它们定义为资源

java - 将数组元素从字符串转换为整数

c# - C# 中的 BesselK 函数

c++ - 这是包含源文件的好习惯吗?

C:将儒略日期转换为公历日期

pdf - 动画幻灯片转换为静态 PDF

jsf - selectOneMenu 在提交后始终显示列表中的最后一项作为所选项目