类型 'double' 和 'const char[5]' 到二进制 'operator<<' 的无效操作数
Invalid operand of types 'double' and 'const char[5]' to binary 'operator<<'
我正在尝试制作一个 C++ Acidity/Base 通用计算器。在尝试完成我的代码时,我发现了以下 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 read , Solution 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;
}
}
运算符<<
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';
我正在尝试制作一个 C++ Acidity/Base 通用计算器。在尝试完成我的代码时,我发现了以下 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 read , Solution 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;
}
}
运算符<<
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';