【问题标题】:VSCode: clang: error: linker command failed with exit code 1 (use -v to see invocation)VSCode:clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
【发布时间】: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 &lt;string&gt; 以获得最大的便携性。
  • @PaulSanders 嘿,保罗,原来我打错了 :( 我声明了 findnumchar() 但随后在代码的下方写下了标题为 findnumchnar。我知道它会像那样小
  • 字符串不是 iostream 'library' 的一部分,如果你使用std::string,你应该包含&lt;string&gt;,这就是它所在的地方。
  • @john 抱歉回复晚了,但是我确实使用了

标签: c++ string function clang


【解决方案1】:

有一个简单的错字,这就是我收到错误的原因。对不起各位,不过还是谢谢你的回答!我将 findnumchar 定义为 findnumchnar。总是检查愚蠢的东西

【讨论】:

    【解决方案2】:

    我有 findnumchnar() 而不是声明的 findnumchar()

    【讨论】:

      【解决方案3】:

      因为 std::string::npos 是 64 位常量,而 found 是 32 位整数,编译器会抱怨 while 循环内部的条件永远不会为真(因为理论上 found 永远不会变大足以等于 std::string::npos)。要解决此问题,只需将 int 替换为 unsigned long long int。完整代码见下面sn-p:

          #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)
          {
              unsigned long long 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;
          }
      
      

      【讨论】:

      • 仍然收到相同的“退出代码 1”错误,不过谢谢!
      猜你喜欢
      • 2018-08-26
      • 2016-12-25
      • 2017-02-09
      • 1970-01-01
      • 2022-01-20
      • 2019-09-28
      • 2016-03-24
      • 2016-02-12
      相关资源
      最近更新 更多