【发布时间】:2020-08-19 05:10:10
【问题描述】:
我已经为我所在的一个类编写了一个代码,它应该拆分一个字符串并显示第二个字符串中有多少个唯一字符。但是,每次我尝试构建代码时,我都会收到“clang: error: linker command failed with exit code 1 (use -v to see invocation)”。我查看了有关此错误的其他问题,但没有找到 VSCode 的解决方案(如果有任何区别,则在 MacBook Pro 上)。有人看到我的错误在哪里吗?还有一个子问题:我是否一定需要#include 字符串?我被告知 std::string 是 iostream 库的一部分。如果我不需要字符串库,我什么时候需要字符串?谢谢大家。
#include <iostream>
#include <string>
int splitwords(std::string, char);
int findnumchar(std::string);
int main()
{
std::string txt1("ABCDEF,GHI,JKLMN,OP");
std::string txt2("BACDGABCDAZ");
int result;
char delimiter = ',';
result = splitwords(txt1, delimiter);
result = findnumchar(txt2);
}
int splitwords(std::string txt, char delimiter)
{
int start, found, cnt = 0;
std::string splitstr;
start = 0;
while ((found = txt.find(delimiter, start)) != std::string::npos)
{
splitstr = txt.substr(start, found - start);
std::cout << "Split Word" << splitstr << std::endl;
start = found + 1;
cnt += 1;
}
splitstr = txt.substr(start, txt.length() - start);
std::cout << "Split Word" << splitstr << std::endl;
return cnt + 1;
}
int findnumchnar(std::string txt)
{
int uniquecnt = 0, index;
int seen[26] = {0};
std::string::iterator iter;
for (iter = txt.begin(); iter < txt.end(); iter++)
{
index = *iter - 'A';
if (seen[index] == 0)
{
seen[index] = 1;
uniquecnt+=1;
}
}
for (int i = 0; i <= 26; i++)
{
if (seen[i] == 1)
{
std::cout << static_cast<char>(i + 'A') << "\t";
}
}
std::cout << std::endl;
std::cout << "The number of unique characters: " << uniquecnt << std::endl;
return uniquecnt;
}
【问题讨论】:
-
完整的错误信息是什么?知道链接器找不到哪些符号会有所帮助。是的,您应该
#include <string>以获得最大的便携性。 -
@PaulSanders 嘿,保罗,原来我打错了 :( 我声明了 findnumchar() 但随后在代码的下方写下了标题为 findnumchnar。我知道它会像那样小
-
字符串不是 iostream 'library' 的一部分,如果你使用
std::string,你应该包含<string>,这就是它所在的地方。 -
@john 抱歉回复晚了,但是我确实使用了