c++ - 在向量练习中为架构 x86_64 编译 C++ 未识别符号
问题描述
(更新了新代码,但同样的问题仍然存在)
因此,为我的 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 函数有关,因为代码在我创建它之前正在编译和运行。
解决方案
比较两行:
void reportNames(vector<string> v, char letter);
void reportName(vector<string> names, char letter) {
...
您用一个名称声明了一个函数,但用另一个名称定义了一个函数。
推荐阅读
- mongodb - find documents for given two dates, by start_date and end_date properties in mongodb
- antd - ant design 是否有类似于 material-ui 的断点检查?
- c# - 使用 Docker .NET Core 映像构建 WinForms C# 应用程序会导致错误:.NETFramework,Version=v4.6.1 were not found
- c++ - Pass Array and Assign in C++ Class
- c - 为什么我的 getline() 没有读取 C 中标准输入的第一行?
- git - 如何使用 git rebase --empty=drop
- batch-file - 有什么技巧可以让我的 CMD 游戏看起来更好吗?
- sql - ASP.NET 字符串值作为查询命令
- php - Woocommerce 删除退款订单的下载权限,使用 wc 核心功能中的查询触发器
- r - 将Long Form数据转换为R中的三维数组