错误! 'float' 和 'const char [2]' 类型的无效操作数到二进制 'operator<<' [关闭]

Posted

技术标签:

【中文标题】错误! \'float\' 和 \'const char [2]\' 类型的无效操作数到二进制 \'operator<<\' [关闭]【英文标题】:ERROR! invalid operands of types 'float' and 'const char [2]' to binary 'operator<<' [closed]错误! 'float' 和 'const char [2]' 类型的无效操作数到二进制 'operator<<' [关闭] 【发布时间】:2020-11-28 17:45:14 【问题描述】:

我正在尝试制作一个相当于两个分数的四函数计算器的程序。错误出现在 cout 语句中的每个 switch 案例中。 错误是:

[错误] 'float' 和 'const char [2]' 类型的无效操作数 二进制'运算符

我尝试谷歌搜索,没有找到,但我确实发现了相同的错误,但使用 int 而不是 float。不幸的是,他犯了一个明显的错误,他甚至没有使用 cout 语句并将

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

using namespace std;

main()
   //This Program is an equivalent of a four function calculator for fraction
    float N1, D1, N2, D2;
    char OP, slash;
    system("cls");
    
    cout<<"Hint: Enter Both Fractions with Operator in between; 1/2 + 6/3";
    cout<<"\nEnter Fractionaly Binary Operation: ";
    cin>>N1>>slash>>D1>>OP>>N2>>slash>>D2;

    if(slash=='/')
    
        switch(OP) 
        
            case '+':
                cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
                break;

            case '-':
                cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)-(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
                break;

            case '*':
                cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<((N1*N2)<<"/"<<(D1*D2))<<" = "<<((N1*N2)/(D1*D2))<<endl;
                break;

            case '/':
                cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<((N1*D2)<<"/"<<(D1*N2))<<" = "<<((N1*D2)/(D1*N2))<<endl;
                break;

            default:
                cout<<"INVALID OPERATOR!";
                getch();
                system("cls");
                return(0);
        
    
    else
    
        cout<<"INVALID INPUT!";
        getch();
        system("cls");
        return(0);
    
    
    
    getch();
    system("cls");
    return(0);

同样的错误出现在以下几行中..

                case '+':
                    cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
                    break;
    
                case '-':
                    cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)-(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
                    break;
    
                case '*':
                    cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<((N1*N2)<<"/"<<(D1*D2))<<" = "<<((N1*N2)/(D1*D2))<<endl;
                    break;
    
                case '/':
                    cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<((N1*D2)<<"/"<<(D1*N2))<<" = "<<((N1*D2)/(D1*N2))<<endl;

我必须将此作为作业提交。

这是我想要的界面。注意:这是使用 cout 语句进行的,后端没有计算。

【问题讨论】:

你只是有很多不匹配的括号。尝试将中间值存储在变量中以提高代码清晰度。 main 在 C++ 中的返回类型必须为 int 我检查了所有括号,它们是匹配的。似乎没有任何不匹配的。关于int,所有其他程序都可以在没有它的情况下工作吗?不管怎样,让我试试吧.. 即使使用 int main 仍然给出相同的错误 @FrançoisAndrieux 我为所需界面添加了一个模型。看看吧,也许你会比我更了解这个程序。 :D 【参考方案1】:

检查括号:

cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "
<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
  ^                               ^                     

试试这个:

    case '+':
        cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<((N1*D2)+(N2*D1))<<"/"<<(D1*D2)<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
        break;

    case '-':
        cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<((N1*D2)-(N2*D1))<<"/"<<(D1*D2)<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
        break;

    case '*':
        cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<(N1*N2)<<"/"<<(D1*D2)<<" = "<<((N1*N2)/(D1*D2))<<endl;
        break;

    case '/':
        cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<(N1*D2)<<"/"<<(D1*N2)<<" = "<<((N1*D2)/(D1*N2))<<endl;
        break;

【讨论】:

【参考方案2】:

括号不匹配

cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<

(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))   /*<-here*/

<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
                

(((N1*D2)+(N2*D1))&lt;&lt;"/"&lt;&lt;(D1*D2))在一个街区内。

【讨论】:

以上是关于错误! 'float' 和 'const char [2]' 类型的无效操作数到二进制 'operator<<' [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

为啥将“float**”转换为“const float**”时出现错误?

错误:无法使用“const char [34]”类型的左值初始化“const char”类型的返回对象

const_cast((char * const)not lvalue?

从'const char *'到'char *'的无效转换[-fpermissive]; VTK-7.1.1编译错误

奇怪的错误,错误:从‘const char*’到‘char’的无效转换[-fpermissive]

全局 const char * 上的错误 LNK1169