0

I'm very new to all of this so forgive me if I format incorrectly or ask a dumb question. Currently in school taking a C++ intro class and my prof has been no help with this. Writing a program to process an input file filled with various job titles and salary ranges (low to high) using three functions specified by the assignment (getData, getAvg, and getLowestAndHighestSalary). In building the program as I go in an attempt to catch any spelling errors/dumb mistakes as they appear, I noticed that as soon as I tried to reference the getData and getLowestAndHIghestSalary functions, it kicks back the error:

error LNK2019: unresolved external symbol "void __cdecl getData(class std::basic_ifstream<char,struct std::char_traits > &,class std::basic_string<char,struct std::char_traits,class std::allocator >,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) referenced in function _main

followed by a LNK2020 error. My understanding is that these types of errors usually start a cascade so solving the first error will usually solve the rest, so I've focused on LNK2019. So far as I can tell, it's not recognizing my function definitions.

I've tried playing with settings, changing linker subsystem from console to windows and back, changing configuration type from application(.exe) to dynamic library(.dll) and back, as well as taking the function definitions and declarations to a header file and still no luck.

Anyone have any ideas on how to make the program recognize my functions?

Code:

#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;
        }
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 3
    the function you declare is different from the one you define. `void getData(ifstream&, string, double, int&);` has `string` as second argument. `void getData(ifstream& inFile, string jobs[], double salaryRange[][2], int& numJobs)` has pointer to string as second argument and different 3rd and 4th arguments – 463035818_is_not_an_ai Nov 14 '20 at 14:16

2 Answers2

1

There are lots of errors in your code where you try to call a function with an array. For instance

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

which should be

getData(inFile, jobs, salaryRange, numJobs);

It's a very common newbie misunderstanding that jobs[20] means the whole jobs array, but it doesn't. It actually means what it says, jobs[20] means the element of the jobs array at position 20.

You make the same error several times.

Once you've fixed those errors, you can fix the prototypes

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

should be

void getData(ifstream&, string[], double[][2], int&);
john
  • 85,011
  • 4
  • 57
  • 81
0

Function declaration and definition are different.

Declaration:

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

Definition:

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

I hope fixing any one of them will fix your issue. Usually you can just copy the definition and paste it with a semicolon to make it a declaration. Like this:

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