Linker error : ld: symbol(s) not found for architecture x86_64

Linker error : ld: symbol(s) not found for architecture x86_64

我正在尝试使用 Mac 上的终端编译以下代码。代码是用 Sublime Text 编写的。

#include <iostream>
#include <fstream>
#include <vector> 
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <string> 
#include <locale> 
#include <cstdlib>

using namespace std;

void parse(string s, vector<string> q);

int main(int argc, char* argv[])
{

    cout << argv[1] << endl;
    cout << argv[2] << endl;
    cout << argv[3] << endl;
    cout << argv[4] << endl;

    int h = stoi(argv[1]);
    int k, c;
    string query;
    vector<string> query_words;
    if(!strcmp(argv[2], "-k"))
    {
        k = stod(argv[3]);
    }

    if(!strcmp(argv[2], "-c"))
    {
        c = stod(argv[3]);
    }


    query = argv[4];
    parse(query, query_words);

    return 0;
}


void parse(string s, vector<string> &q)
{

    string x;
    for(size_t i = 0; i < s.length(); i++)
    {
        string temp;
        if(s[i] == '_')
        {
            s[i] = ' ';
        }

        if(!isalnum(s[i]) && s[i] != ' ')
        {
            temp = s.substr(i+1, s.length() - i-1); 
            x = s.substr(0, i) +  temp;
            s = x;
            i--;
        }
    }

    int start_subString = 0;
    for(size_t i = 0; i < s.length(); i++)
    {
        if(s[i] == ' ')
        {
            string temp = s.substr(start_subString, i - start_subString);
            start_subString = i+1;
            q.push_back(temp);

        }

        if(i+1 == s.length())
        {
            string temp = s.substr(start_subString, i + 1 - start_subString);
            q.push_back(temp);

        }
    }


}

我尝试使用以下行进行编译:

g++ -std=c++11 hits.cpp -o hits

这会导致以下错误:

Undefined symbols for architecture x86_64:
  "parse(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >)", referenced from:
      _main in hits-d8b25c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

您声明:

void parse(string s, vector<string> q);

所以,这就是它所说的。不幸的是,你定义:

void parse(string s, vector<string> &q)

这不是同一个函数,这就是为什么链接器无法找到它正在寻找的函数的原因——您还没有定义它。最有可能的是,您只需要修正声明即可。