【问题标题】:No instance of overloaded function toupper matches the argument list [duplicate]没有重载函数 toupper 的实例与参数列表匹配
【发布时间】:2020-12-10 08:45:56
【问题描述】:

我正在尝试使用toupper 将字符串中的第一个字母转换为大写,但它一直在下面显示错误代码:

没有重载函数“toupper”的实例与参数列表匹配

代码:

#include<iostream>
#include<cstring>
#include<cctype>
using namespace std ;

int main()
{
    string s("some string");

    if(s.begin() != s.end()){
        auto c = s.begin();
        c = toupper (c);
    }
    return 0 ;
}

【问题讨论】:

  • c 是指向第一个字符的迭代器,而不是字符。你需要先取消引用它
  • s.begin() 不是字符,它是一个可以解引用为字符的迭代器,auto c = toupper(*s.begin()); 也是如此
  • 除了已经说过的,请注意这里,您修改的是c,而不是字符串本身。你可以使用s[0]=toupper(s[0]);
  • 使用front而不是begin,如上所述,使用引用来更改字符串本身而不是变量,即auto&amp; c = s.front();
  • 这是C++,请改用&lt;string&gt;&lt;cstring&gt;

标签: c++ c++11 stdstring


【解决方案1】:

使用* 操作符取消对迭代器的引用就可以了:

*c = toupper (*c);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多