链接器不喜欢我的函数定义(LNK2019未解析的外部符号)
Linker doesn't like my function definition (LNK2019 Unresolved External Symbol)
我对这一切都很陌生,所以如果我的格式不正确或问了一个愚蠢的问题,请原谅我。目前正在学校上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);
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector