首页 > 解决方案 > 'double' 和 'const char[5]' 类型的无效操作数到二进制 'operator<<'

问题描述

我正在尝试制作一个 C++ 酸度/碱度通用计算器。在尝试完成我的代码时,我遇到了以下 5 个错误;

main.cpp: In function 'int main()':
main.cpp:62:36: error: invalid operands of types 'double' and 'const char [5]' to binary 'operator<<'
    cout << "[H+]=" << 10^(W-X)  << "*10^"  << W << '\n';
                                    ^

main.cpp:63:34: error: invalid operands of types 'int' and 'double' to binary 'operator^'
    cout << "[OH-]=" << 1/(10^(W-X))  << "*10^"  << (-14)-W << '\n'; 
                                  ^
main.cpp:77:33: error: invalid operands of types 'int' and 'double' to binary 'operator^'
    cout << "[H+]=" << 1/(10^(U-V))  << "*10^"  << (-14)-U << '\n';
                                 ^
main.cpp:78:37: error: invalid operands of types 'double' and 'const char [5]' to binary 'operator<<'
    cout << "[OH-]=" << 10^(U-V)  << "*10^"  << U << '\n'; 
                                     ^

D:\EvaxHybrid\Mywork\Cpp\ChempHpOH\Makefile.win:28: recipe for target 'main.o' failed

mingw32-make.exe: *** [main.o] Error 1

我已经尝试实现不是内联的解决方案 1 并且会使代码太复杂而无法阅读解决方案 2 不是内联的并且不是同一个问题(我没有使用新的)。如果没有其他选择,任何人都可以对此发表评论,我会做一个与功能相关的方法。
这是代码(main.cpp);

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <conio.h>
#include <cmath>

using namespace std;

int main() {
    cout << "Choose Start point..." << '\n';
    cout << "1. [H+]\n2. [OH-]\n3. pH\n4. pOH\n";
    int choice;
    cin >> choice;
    system ("cls");
    switch(choice)
    {
        case 1:
            cout << "Convert [H+] to Scientific Notation of A*10^B and input A,B\n";
            system ("pause");
            cout << '\n';
            double A,B;
            cout << "A:";
            cin >> A;
            cout << '\n';
            cout << "B:";
            cin >> B;
            cout << '\n';
            cout << "[H+]=" << A  << "*10^"  << B << '\n';
            cout << "[OH-]=" << 1/A  << "*10^"  << (-14)-B << '\n'; 
            cout << "[pH]=" << (-log10(A)-B) << '\n';
            cout << "[pOH]=" << 14-(-log10(A)-B) << '\n';
            break;
        case 2:
            cout << "Convert [OH-] to Scientific Notation of Z*10^Y and input Z,Y\n";
            system ("pause");
            cout << '\n';
            double Z,Y;
            cout << "Z:";
            cin >> Z;
            cout << '\n';
            cout << "Y:";
            cin >> Y;
            cout << '\n';
            cout << "[H+]=" << 1/Z  << "*10^"  << (-14)-Y << '\n';
            cout << "[OH-]=" << Z  << "*10^"  << Y << '\n'; 
            cout << "[pH]=" << 14-(-log10(Z)-Y) << '\n';
            cout << "[pOH]=" << (-log10(Z)-Y) << '\n';
            break;
        case 3:
            cout << "Input pH as X\n";
            system ("pause");
            cout << '\n';
            double X;
            cout << "X:";
            cin >> X;
            double W;
            W = -ceil(X);
            cout << '\n';
            cout << "[H+]=" << 10^(W-X)  << "*10^"  << W << '\n';
            cout << "[OH-]=" << 1/(10^(W-X))  << "*10^"  << (-14)-W << '\n'; 
            cout << "[pH]=" << X << '\n';
            cout << "[pOH]=" << 14-X << '\n';
            break;
        case 4:
            cout << "Input pOH as V\n";
            system ("pause");
            cout << '\n';
            double V;
            cout << "V:";
            cin >> V;
            double U;
            U = -ceil(V);
            cout << '\n';
            cout << "[H+]=" << 1/(10^(U-V))  << "*10^"  << (-14)-U << '\n';
            cout << "[OH-]=" << 10^(U-V)  << "*10^"  << U << '\n'; 
            cout << "[pH]=" << 14-V << '\n';
            cout << "[pOH]=" << V << '\n';
            break;
    }
}

标签: c++dev-c++

解决方案


运算符<< 优先于运算符^

cout << "[H+]=" << 10^(W-X)  << "*10^"  << W << '\n';

读作

(cout << "[H+]=" << 10)  ^  ((W-X)  << "*10^"  << W << '\n');

放括号:

cout << "[H+]=" << (10^(W-X))  << "*10^"  << W << '\n';

推荐阅读