链接器不喜欢我的函数定义(LNK2019未解析的外部符号)

Linker doesn't like my function definition (LNK2019 Unresolved External Symbol)

本文关键字:外部 符号 LNK2019 不喜欢 我的 函数 定义 链接      更新时间:2024-09-28

我对这一切都很陌生,所以如果我的格式不正确或问了一个愚蠢的问题,请原谅我。目前正在学校上C++入门课,我的教授对此没有任何帮助。编写一个程序,使用作业指定的三个函数(getData、getAvg和getLowestAndHighestSalary(来处理填充有各种职位和工资范围(从低到高(的输入文件。在构建程序的过程中,我试图捕捉出现的任何拼写错误/愚蠢错误,我注意到,当我试图引用getData和getLowestAndHIghestSalary函数时,它会立即返回错误:

错误LNK2019:未解析的外部符号";void __cdecl getData(类std::basic_ifstream<char,struct-std::char_traits>amp;,班std::basic_string<char,struct-std::char_traits,类std::分配器>,double,int&("(?getData@@YAXAAV$basic_ifstream@DU$char_traits@D@std@@@std@@V$basic_string@DU$char_traits@D@std@@V$allocator@D@2@@2@NAAH@Z)在函数_main 中引用

后面跟着一个LNK2020错误。我的理解是,这些类型的错误通常会引发级联,所以解决第一个错误通常会解决其余的错误,所以我专注于LNK2019。据我所知,它没有识别我的函数定义。

我尝试过使用设置,将链接器子系统从控制台更改为windows并返回,将配置类型从应用程序(.exe(更改为动态库(.dll(并返回,以及将函数定义和声明转换为头文件,但仍然没有成功。

有人知道如何让程序识别我的功能吗?

代码:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void getData(ifstream&, string, double, int&);
double getAverage(double&, double&);
void getLowestAndHighestAvgSalary(double&, int&, int&);
int main(void) {
ifstream inFile;
string jobs[20];
double salaryRange[20][2], avgSalary[20];
int numJobs = 0, lowest_index = 0, highest_index = 0;
char input;
getData(inFile, jobs[20], salaryRange[20][2], numJobs);
for (int i = 0; i < 19; i++) {
avgSalary[i] = getAverage(salaryRange[i][0], salaryRange[i][1]);
}
getLowestAndHighestAvgSalary(avgSalary[20], lowest_index, highest_index);
cout << "Would you like to output to console (a) or output file (b)?n";
cin >> input;
if (input == 'a') {
for (int i = 0; i < 19; i++) {
cout << "The average salary of the " << jobs[i] << "is $" << avgSalary[i] <<endl;
}
cout << "nThe lowest average salary is " << avgSalary[lowest_index] << " (" << jobs[lowest_index] << ")";
cout << "nThe highest average salary is " << avgSalary[highest_index] << " (" << jobs[highest_index] << ")";
}
else if (input == 'b') {
ofstream outFile;
outFile.open("job_stat.txt");
for (int i = 0; i < 19; i++) {
outFile << "The average salary of the " << jobs[i] << "is $" << avgSalary[i] << endl;
}
outFile << "nThe lowest average salary is " << avgSalary[lowest_index] << " (" << jobs[lowest_index] << ")";
outFile << "nThe highest average salary is " << avgSalary[highest_index] << " (" << jobs[highest_index] << ")";
}
return 0;
}
void getData(ifstream& inFile, string jobs[], double salaryRange[][2], int& numJobs) {
inFile.open("job_salaries.txt");
for (int i = 0; i < 19; i++) {
if (inFile.eof()) {
break;
}
getline(inFile, jobs[i]);
inFile >> salaryRange[i][0];
inFile.ignore(3);
inFile >> salaryRange[i][1];
inFile.ignore(100, 'n');
numJobs = i;
}
}
double getAverage(double& lowSalary, double& highSalary) {
double avgSalary;
avgSalary = (lowSalary + highSalary) / 2;
return avgSalary;
}
void getLowestAndHighestAvgSalary(double avgSalary[], int& lowest_index, int& highest_index) {
highest_index = 0;
lowest_index = 0;
double temp = avgSalary[0];
for (int i = 0; i < 19; i++) {
if (avgSalary[i] > temp) {
highest_index = i;
}
}
for (int i = 0; i < 19; i++) {
if (avgSalary[i] < temp) {
lowest_index = i;
}
}
}

在试图用数组调用函数的代码中有很多错误。例如

getData(inFile, jobs[20], salaryRange[20][2], numJobs);

应该是

getData(inFile, jobs, salaryRange, numJobs);

这是一个非常常见的新手误解jobs[20]意味着整个jobs数组,但它不是。它实际上意味着它所说的,jobs[20]意味着jobs阵列在位置20处的元素。

你多次犯同样的错误。

一旦你修复了这些错误,你就可以修复的原型

void getData(ifstream&, string, double, int&);

应该是

void getData(ifstream&, string[], double[][2], int&);

函数声明和定义不同。

声明:

void getData(ifstream&, string, double, int&);

定义:

void getData(ifstream& inFile, string jobs[], double salaryRange[][2], int& numJobs)

我希望解决其中任何一个问题都能解决你的问题。通常,您只需复制定义并粘贴分号即可使其成为声明。像这样:

void getData(ifstream& inFile, string jobs[], double salaryRange[][2], int& numJobs);