gpt4 book ai didi

c++ - 从 char* 到 int 的无效转换。 C++ 错误;

转载 作者:行者123 更新时间:2023-11-30 01:44:08 25 4
gpt4 key购买 nike

<分区>

我在这里很困惑。为什么会出现此错误?

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 还是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com