提问者:小点点

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


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

错误LNK2019:无法解析的外部符号“void__cdecl getData(class std::basic_ifstream&,class std::basic_string,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,将配置类型从application(。exe)更改为dynamic library(。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;
        }
    }
}

共2个答案

匿名用户

在尝试用数组调用函数时,代码中会出现很多错误。例如

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);