【问题标题】:No matching function call in C++C++ 中没有匹配的函数调用
【发布时间】:2013-05-21 05:21:17
【问题描述】:

我收到“没有匹配的函数调用”的错误,有什么想法吗?提前致谢。

#include <iostream>
#include <string>
using namespace std;

void redactDigits(string & s);

int main(int argc, const char * argv[])
{

    redactDigits("hello");

    return 0;
}

void redactDigits(string & s){

double stringLength = 0;
string copyString; 

stringLength = s.size();

for (int i = 0; i < stringLength + 1; i++) {
    if (atoi(&s[i])) {
        copyString.append(&s[i]);
    }

    else {
        copyString.append("*");
    }


}

s = copyString;

cout << s; 

}

【问题讨论】:

    标签: c++ error-handling


    【解决方案1】:

    您的函数声明中缺少void。此外,您需要传递 const 引用,以便能够绑定到临时:

    void redactDigits(const string & s);
    ^^^^              ^^^^^
    

    没有const,这个调用是非法的:

    redactDigits("hello");
    

    虽然有些编译器有非标准扩展,允许非常量引用绑定到临时对象。

    编辑:由于您正在尝试修改函数内的输入字符串,另一种解决方案是保留原始函数签名并将其传递给 std::string 而不是以空字符结尾的字符串文字,或者只返回一个 std::string:

    std::string redactDigits(const std::string& s)
    {
      ...
      return copyString;
    }
    

    然后

    std::string s = redactDigits("hello");
    

    【讨论】:

    • @user1681673 包括const?
    猜你喜欢
    • 2021-09-21
    • 2016-11-23
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多