【发布时间】:2023-03-27 13:00:02
【问题描述】:
我在使用 toupper() 函数时遇到问题:
代码:
#include <iostream>
#include <string>
using namespace std;
int main (){
string input {"ab"};
string output {""};
cout << output + toupper(input[0]);
return 0;
}
错误是:
没有运算符“+”与这些操作数匹配——操作数类型为:std::_cx11::string + int。
但如果我写:
#include <iostream>
#include <string>
using namespace std;
int main (){
string input {"ab"};
string output {""};
char temp = toupper(input[0]);
cout << output + temp;
return 0;
}
它工作正常。谁能告诉我为什么?
【问题讨论】:
-
toupper 返回 int,而不是 char (
int toupper( int ch );),string + int 是不可能的。 -
toupper返回int,而不是char。operator +对std::string和int没有过载,但std::string和char有一个过载。见here。