我正在使用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);
答案 0 :(得分: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