How to fix error LNK2019?

A simple C++ program: How to fix the error?

  • This is an excersise from Teach Yourself C++, here are the files: main.cpp ---------------- #include <iostream> #include "employee.hpp" using namespace std; int main() { Employee emp1, emp2; emp1.SetAge(32); emp1.SetSalary(50000); emp1.SetYears(22); emp2.SetAge(68); emp2.SetSalary(99000); emp2.SetYears(45); cout << "EMPLOYEE 1: Age: " << emp1.GetAge() << endl; cout << "Years of Service: " << emp1.GetYears() << endl; cout << " Salary: " << emp1.GetSalary() << "\n\n"; cout << "EMPLOYEE 2: Age: " << emp2.GetAge() << endl; cout << "Years of Service: " << emp2.GetYears() << endl; cout << " Salary: " << emp2.GetSalary() << "\n\n"; return 0; } employee.hpp --------------------- using namespace std; #ifndef EMPLOYEE_HPP_INCLUDED #define EMPLOYEE_HPP_INCLUDED class Employee { private: unsigned short int age; unsigned short int yearsOfService; unsigned int salary; public: unsigned short int GetAge() const; unsigned short int GetYears() const; unsigned int GetSalary() const; void SetAge(unsigned short int); void SetYears(unsigned short int); void SetSalary(unsigned int); }; #endif // EMPLOYEE_HPP_INCLUDED employee.cpp -------------------- #include <iostream> #include "employee.hpp" using namespace std; unsigned short int Employee::GetAge() {return age;} unsigned short int Employee::GetYears() {return yearsOfService;} unsigned int Employee::GetSalary() {return salary;} void Employee::SetAge(unsigned short int Age) {age = Age;} void Employee::SetYears(unsigned short int Years) {yearsOfService = Years;} void Employee::SetSalary(unsigned int Salary) {salary = Salary;} These are the error reports: C:\Documents and Settings\Administrator\My Documents\C++ Projects\Day 6 Exercises\employee.cpp|7|error: prototype for 'short unsigned int Employee::GetAge()' does not match any in class 'Employee'| C:\Documents and Settings\Administrator\My Documents\C++ Projects\Day 6 Exercises\employee.hpp|11|error: candidate is: short unsigned int Employee::GetAge() const| C:\Documents and Settings\Administrator\My Documents\C++ Projects\Day 6 Exercises\employee.cpp|8|error: prototype for 'short unsigned int Employee::GetYears()' does not match any in class 'Employee'| C:\Documents and Settings\Administrator\My Documents\C++ Projects\Day 6 Exercises\employee.hpp|12|error: candidate is: short unsigned int Employee::GetYears() const| C:\Documents and Settings\Administrator\My Documents\C++ Projects\Day 6 Exercises\employee.cpp|9|error: prototype for 'unsigned int Employee::GetSalary()' does not match any in class 'Employee'| C:\Documents and Settings\Administrator\My Documents\C++ Projects\Day 6 Exercises\employee.hpp|13|error: candidate is: unsigned int Employee::GetSalary() const| ||=== Build finished: 6 errors, 0 warnings ===| I'm stumped, I don;t know how to fix these errors.

  • Answer:

    They forgot to specify "const" in the employee.cpp file. Copy this code in place of what you have in employee.cpp. You'll notice that GetAge(), GetYears(), and GetSalary() all had it defined as a const return value in the employee.hpp file. #include <iostream> #include "employee.hpp" using namespace std; unsigned short int Employee::GetAge() const {return age;} unsigned short int Employee::GetYears() const {return yearsOfService;} unsigned int Employee::GetSalary() const {return salary;} void Employee::SetAge(unsigned short int Age) {age = Age;} void Employee::SetYears(unsigned short int Years) {yearsOfService = Years;} void Employee::SetSalary(unsigned int Salary) {salary = Salary;}

faceless... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

add const behind the function names before the parameter list unsigned short int Employee::GetAge() const {return age;} unsigned short int Employee::GetYears() const {return yearsOfService;} unsigned int Employee::GetSalary() const {return salary;}

the prototype declared const functions and you provided non-const functions. unsigned short int GetAge() const; unsigned short int GetYears() const; unsigned int GetSalary() const; The following changes should make your code work ... unsigned short int Employee::GetAge() const {return age;} unsigned short int Employee::GetYears() const {return yearsOfService;} unsigned int Employee::GetSalary() const {return salary;}

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.