0

我正在使用 Xcode,它在传递对字符串对象本身的引用时拒绝编译。

(string &text, string remove)

没有&它编译。是我的代码错误还是项目文件有问题?

这是编译器错误:

Undefined symbols for architecture i386:
"removeLetters3(std::string, std::string)", referenced from:
  Main() in narcissism.o
 ld: symbol(s) not found for architecture i386
 clang: error: linker command failed with exit code 1 (use -v to see invocation)

代码:

string removeLetters3(string &text, string remove)
{
for (int i = 0; i < remove.length(); i++)
{
    int pos = 0;

    while ((pos = text.find(remove[i], pos)) != string :: npos)
    {
        text.replace(pos, 1, "");
    }
}
return "";
}

这是函数的调用方式:

string text;
string rletter;
removeLetters3(text, rletter);
4

1 回答 1

3

您的代码没有任何问题,但从您的错误来看,我认为调用者看到的声明是这样的:

string removeLetters3(string text, string remove);

虽然您的实现读取

string removeLetters3(string &text, string remove) {

因此,当您的链接器试图找到第一个的实现时,它会失败,因为没有这样的实现。

错误证明了这一点:

Undefined symbols for architecture i386:
"removeLetters3(std::string, std::string)", referenced from:
  Main() in narcissism.o

注意签名。它不是说:

Undefined symbols for architecture i386:
"removeLetters3(std::string&, std::string)", referenced from:
  Main() in narcissism.o
于 2012-11-09T01:51:42.347 回答