【问题标题】:Invalid user-defined conversion: C++ error无效的用户定义转换:C++ 错误
【发布时间】:2019-06-16 18:50:10
【问题描述】:

我正在使用 Code::Blocks 作为我的 IDE 用 C++ 制作一个(非常)简单的计算器程序。我在我的程序中遇到了几个错误。请看一下我的代码并告诉我错误是什么。谢谢。

#include <iostream>
#include <limits>
#include <conio.h>

int num1;
char Operator;
int num2;


void sum() {
    std::cin >> num1; // User inputs first number
    std::cin >> Operator; // User inputs operator
    std::cin >> num2; // User inputs second number

    // These if statements identify the operator and perform the appropriate 
    // operation

    if ( Operator == '+' ) {
        std::cout << num1 + num2;
     }

    else if ( Operator == '-' ) {
        std::cout << num1 - num2;
    }

    else if ( Operator == '*' ) {
         std::cout << num1 * num2;
    }

    else if ( Operator == '/' ) {
        std::cout << num1 / num2;
    }

    else {
        std:: cout << "Incorrect value/s entered.";
    }
}

int main {
    std::cout << "Press q to quit the program.";

    while(1) {
        sum()

        if(ascii_value==113) { // For Q
            break;
        }
    }

    return 0;
}

错误:

error: invalid user-defined conversion from 'std:: basic_ostream<char>' to 
'int' [-fpermissive]
error: expected unqualified-id before 'while'

我在四天前才开始学习 C++,所以请感谢我对错误了解不多的事实。另外,我不确定是否需要包括限制,所以请在下面的 cmets 中告诉我。

【问题讨论】:

  • 输入是什么?
  • 第一个是第一个数字,第二个是运算符,第三个是第二个数字。
  • 您是否要求使用修复此代码的编译?似乎 main 没有正确声明并且缺少分号...
  • 多亏了遗忘的回答,我才意识到这一点,很抱歉我之前没有注意到这一点,但我刚刚开始掌握这种语言。
  • @SamuelCrawford 我已回滚您的编辑,因为它们使接受的答案无效。如果您还有其他问题,您应该提出一个新问题。

标签: c++ calculator user-defined


【解决方案1】:

int main不是函数声明,改为

int main()

sum() 需要一个分号。 在if(ascii_value==113) 中,ascii_value 在代码中没有定义

【讨论】:

  • @OznOg 错过了那个 :)
猜你喜欢
  • 1970-01-01
  • 2018-02-08
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2012-05-07
  • 2019-12-19
相关资源
最近更新 更多