【问题标题】:compiling C++ unidentified symbols for architecture x86_64 in vector exercise在向量练习中为架构 x86_64 编译 C++ 未识别符号
【发布时间】:2020-08-16 13:46:51
【问题描述】:

(更新了新代码,但同样的问题仍然存在)

所以为我的 C++ 课做了一些功课,几乎完成了,但它不会编译并返回以下错误消息:

Undefined symbols for architecture x86_64:
  "reportNames(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> > > >, char)", referenced from:
      _main in CHomeWork11-55ac9e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

程序代码如下:

#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
ifstream fin;

int getRank(vector<string> names, string choice);
void reportNames(vector<string> names, char letter);

int main ()

{
  string name;
  int counter = 0;
  string choice;
  bool on = true;
  char letter;

 vector <string> names;
 vector <string>::iterator iter;
 vector <string>::iterator iterF;

    fin.open("GirlNames.txt");  // reads in data

    getline(fin, name);

    while (!fin.fail())
    {
          names.push_back(name);
          getline(fin, name);
    }
    fin.close();

    cout << "Top Ten Baby Girl Names:" << endl;

    iter = names.begin();

    while(counter !=10)
    {
        cout << *iter << endl;
        iter++;
        counter++;
    }

    while(on)
    {
        cout << "Enter a name (press q to quit)" << endl;
        getline(cin, choice);
        if(choice == "q")
           on = false;

        else
        {
            iterF = find(names.begin(), names.end(), choice);
            if(iterF == names.end())
               cout << choice << " Is not on this list" << endl;
            else
            {
                  cout << choice << " is rank " << getRank(names, choice) << endl;
            }  
        }
    }
    sort(names.begin(), names.end());

    cout << "enter a starting letter for your name:" << endl;
    cin >> letter;
    reportNames(names, letter);
}

int getRank(vector<string> names, string choice)
{
    int rank = 1;
   for(int i = 0; i < names.size(); i++)
   {
       if(names[i] == choice)
           return rank;
        else
            rank++;
   }
   return 0; //dummy return statement for the compiler
}
void reportName(vector<string> names, char letter)
{
    for(int i = 0; i < names.size(); i++)
   {
       if(names[i].find(letter) !=string::npos)
         cout << names[i] << endl;
   }

}

我很确定问题与 reportName 函数有关,因为代码在我创建它之前正在编译和运行。

【问题讨论】:

  • OT:在许多应该通过常量引用传递的函数中,您通过值 vector&lt;string&gt; names 传递。

标签: c++ function vector compiler-errors linker-errors


【解决方案1】:

比较两行:

void reportNames(vector<string> v, char letter);
void reportName(vector<string> names, char letter) {
...

你用一个名字声明了一个函数,但用另一个名字定义了。

【讨论】:

  • 返回并改变了它。仍然吐出同样的错误
  • 这应该可以解决问题。你保存文件了吗?请提供更新后的代码。
  • @RobertHebert,请仔细阅读我的回答。无需重命名参数(参数名称不在函数签名中)。函数本身的名称不正确。声明中有复数 reportNames,定义中有单个 reportName。计算每个名称中的字符数。
  • 哦,我明白了,我太专注于参数名称,没有注意到实际的函数名称拼写错误。真的为此踢自己。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 2013-09-16
  • 2017-03-03
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多