-2

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

#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; 

}
4

1 回答 1

2

您的函数声明中缺少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");
于 2013-05-21T05:22:41.843 回答