【问题标题】:Invalid operand of types 'double' and 'const char[5]' to binary 'operator<<''double' 和 'const char[5]' 类型的无效操作数到二进制 'operator<<'
【发布时间】:2020-06-23 09:28:14
【问题描述】:

我正在尝试制作一个 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

我已经尝试实现 Solution 1 which isn't inline and would make the code too complex to readSolution 2 which isn't inline and not the same problem (I didn't use new)。如果没有其他选择,任何人都可以对此发表评论,我会做一个与功能相关的方法。
这是代码(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;
    }
}

【问题讨论】:

  • ^ 不是电源。解决这个问题,其余问题应该会消失。
  • 10^(W-X) 不会像您认为的那样做。有关详细信息,请参阅您的 C++ 书籍。您不能通过猜测它的样子来尝试提出有效的 C++ 语法。了解它应该是什么的唯一方法是从书中学习。
  • 看来你也忘记了,在 C++ 中,1 除以 2 的结果为零。
  • 在 C# 中,1/2 仍然是 0。不过,我不知道您的代码中是否存在问题,因为它看起来都是int / double ......除非我错过了一个。
  • A 不需要.0,因为它的类型是double...重要的是类型,而不是值

标签: c++ dev-c++


【解决方案1】:

运营商&lt;&lt;has precedence over运营商^

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

读作

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

放括号:

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

【讨论】:

  • 另外,请注意a^b 通常表示xor b。如果要计算指数,请参阅std::pow
  • &lt;&lt; 不会优先于^,因为&lt;&lt; 排在第一位? (只是措辞)
  • 在应用您的解决方案之前,我可能必须先使用 std::pow。
  • 好的,通过结合您的解决方案并将 ^ 替换为 pow() 来修复它。
  • @ChrisMM 我的开头搞砸了,已修复。 *掌心*
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多