簡體   English   中英

VSCode:clang:錯誤:linker 命令失敗,退出代碼為 1(使用 -v 查看調用)

[英]VSCode: clang: error: linker command failed with exit code 1 (use -v to see invocation)

我已經為我所在的一個類制作了一個代碼,它應該拆分一個字符串並顯示第二個字符串中有多少個唯一字符。 但是,每次我嘗試構建代碼時,我都會收到“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;
    }

因為std::string::npos是一個 64 位常量,並且found是一個 32 位 integer,所以編譯器抱怨 while 循環內部的條件永遠不會為真(因為found理論上永遠不會變得足夠大等於 std::string::npos)。 要解決此問題,只需將 int 替換為 unsigned long long int。 請參閱下面的完整代碼片段:

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

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

有一個簡單的錯字,這就是我收到錯誤的原因。 對不起各位,不過還是謝謝你的回答。 我將 findnumchar 定義為 findnumchnar。 總是檢查愚蠢的東西

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM