我在这里感到很困惑。为什么我会收到此错误?
Running /home/ubuntu/workspace/main.cpp
/home/ubuntu/workspace/main.cpp: In function ‘int main()’:
/home/ubuntu/workspace/main.cpp:22:19: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
case "-" :
^
/home/ubuntu/workspace/main.cpp:22:19: error: ‘(int)((long int)"-")’ is not a constant expression
/home/ubuntu/workspace/main.cpp:31:19: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
case "*" :
^
/home/ubuntu/workspace/main.cpp:31:19: error: ‘(int)((long int)"*")’ is not a constant expression
/home/ubuntu/workspace/main.cpp:40:19: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
case "/" :
^
/home/ubuntu/workspace/main.cpp:40:19: error: ‘(int)((long int)"/")’ is not a constant expression
/home/ubuntu/workspace/main.cpp:50:14: error: no match for ‘operator>>’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘const char [41]’)
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
/home/ubuntu/workspace/main.cpp:50:14: note: candidates are:
In file included from /usr/include/c++/4.8/string:53:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.tcc:996:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^
/usr/include/c++/4.8/bits/basic_string.tcc:996:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/istream:882:0,
from /usr/include/c++/4.8/iostream:40,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/bits/istream.tcc:955:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
^
/usr/include/c++/4.8/bits/istream.tcc:955:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/istream:882:0,
from /usr/include/c++/4.8/iostream:40,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/bits/istream.tcc:923:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^
/usr/include/c++/4.8/bits/istream.tcc:923:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/iostream:40:0,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:727:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^
/usr/include/c++/4.8/istream:727:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/iostream:40:0,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:732:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^
/usr/include/c++/4.8/istream:732:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/iostream:40:0,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:774:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^
/usr/include/c++/4.8/istream:774:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/iostream:40:0,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:779:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^
/usr/include/c++/4.8/istream:779:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
In file included from /usr/include/c++/4.8/iostream:40:0,
from /home/ubuntu/workspace/main.cpp:1:
/usr/include/c++/4.8/istream:872:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^
/usr/include/c++/4.8/istream:872:5: note: template argument deduction/substitution failed:
/home/ubuntu/workspace/main.cpp:50:17: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
^
对于这个C ++代码:
#include <iostream>
using namespace std;
int main(){
char option,continueCheck;
int a,b,res;
cout << "********** Welcome to the operation game ********* " <<endl;
do{
cout << "Please type (+,-,/,*) to add your numbers " <<endl;
cin >> option;
switch(option){
case '+' :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a+b;
printf("%d + %d = %d \n" , a,b,res);
break;
case "-" :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a-b;
printf("%d + %d = %d \n" , a,b,res);
break;
case "*" :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a*b;
printf("%d + %d = %d \n" , a,b,res);
break;
case "/" :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a/b;
printf("%d + %d = %d \n" , a,b,res);
break;
}
cout >> "Want to continue? Enter Y or y OR N or n" << endl;
cin >> continueCheck;
}while(continueCheck = 'y');
}
我的意思是我显然没有在任何地方将char
转换为int
。我甚至都没有学会如何做到这一点。就像我在这里感到困惑一样。它认为'+'
是int
还是其他什么?
答案 0 :(得分:3)
在您的情况下,您使用的双引号对应const char*
。您想使用单引号,即char
s。
它抱怨从const char*
转换为int
的原因是char
被提升为int
类型。
但是你的程序还有很多其他问题。
所有printf
打印的内容都相同(75%的时间都不正确)。
您需要使用双等号来检查while
中的相等性。
您在>>
的末尾使用了提取运算符std::cout
- 应为<<
。
这是一个(更多)正确的版本:
#include <iostream>
using namespace std;
int main(){
char option,continueCheck;
int a,b,res;
cout << "********** Welcome to the operation game ********* " <<endl;
do{
cout << "Please type (+,-,/,*) to add your numbers " <<endl;
cin >> option;
switch(option){
case '+' :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a+b;
printf("%d + %d = %d \n" , a,b,res);
break;
case '-' :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a-b;
printf("%d - %d = %d \n" , a,b,res);
break;
case '*' :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a*b;
printf("%d * %d = %d \n" , a,b,res);
break;
case '/' :
cout << "Please type in the first number" <<endl;
cin >> a;
cout << "Please type in the second number" <<endl;
cin >> b;
res = a/b;
printf("%d / %d = %d \n" , a,b,res);
break;
}
cout << "Want to continue? Enter Y or y OR N or n" << endl;
cin >> continueCheck;
}while(continueCheck == 'y');
}
在行动here中查看。
答案 1 :(得分:1)
'-'
和"-"
之间存在实质性差异:
char
且值为45(ASCII值为 - )-
的ASCII代码,0表示字符串的结尾。字符串作为一个整体的值是指向存储它的内存的指针。有效地,您的代码尝试执行的操作是将option
的值与指向内存的指针进行比较。修复方法是更改引号。
您的代码中还有其他错误,最突出的是
while(continueCheck = 'y')
将值'y'
分配给continueCheck
,如果指定的值为非零值(始终为true),则继续循环。您很可能希望将 continueCheck
与y
进行比较,这是通过运算符==
完成的,而不是=
。
答案 2 :(得分:0)
可能是打字错误!? 第22行:编译器在这个位置需要一个整数,但这是一个字符串,一个指针。 第13行:在此输入:' - '