简体   繁体   中英

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

I've made a code for one of my classes I'm in where it should split up a string and show how many unique characters there are in a second string. However, every time I try to build the code I get "clang: error: linker command failed with exit code 1 (use -v to see invocation)". I've looked at other questions for this error and have not found a solution for VSCode (on MacBook Pro if that makes any difference). Does anybody see where my error is? Also sub question: do I necessarily need #include string? I've been told that std::string is part of iostream library. If I don't need string library for this, when is a time I would need string? Thanks everyone.

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

Because std::string::npos is a 64-bit constant, and found is a 32-bit integer, the compiler is complaining that the condition inside of the while loop is never true (since found can never theoretically get big enough to be equal to std::string::npos). To resolve this issue, just replace int with unsigned long long int. See the full code snippet below:

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

I had findnumchnar() instead of the declared findnumchar()

There was a simple typo, that's why I was getting the error. Sorry guys, thanks for answering though. I defined findnumchar as findnumchnar. Always check for stupid stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM