Having trouble with a class constructor.
Apr 25, 2012 at 1:24am UTC
Everytime I'm compiling, I get this error.
1 2
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Employee::Employee(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,double,int)" (??0Employee@@QAE@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@NH@Z) referenced in function "public: void __thiscall Employee::`default constructor closure'(void)" (??_FEmployee@@QAEXXZ)
1>H:\Computer Science I\Program 10.4.8b\Debug\Program 10.4.8b.exe : fatal error LNK1120: 1 unresolved externals
The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Employee
{
private :
int id;
string name;
double pay;
int hours;
public :
Employee(int = 0, string = " " , double = 0, int = 0);
void enterData(int idNum)
{
id = idNum;
cout << endl << "Enter employee name: " ;
getline(cin, name);
cout << endl << "Enter a pay rate: " ;
cin >> pay;
cout << endl << "Enter hours per week: " ;
cin >> hours;
system("cls" );
cout << "Data entered successfully." << endl << endl;
}
void changeData()
{
cout << endl << "Enter employee name: " ;
getline(cin, name);
cout << endl << "Enter a pay rate: " ;
cin >> pay;
cout << endl << "Enter hours per week: " ;
cin >> hours;
system("cls" );
cout << "Data changed successfully." << endl << endl;
}
void showData()
{
cout << "Employee ID #: " << id << endl
<< "Employee Name: " << name << endl
<< "Pay Rate: " << pay << endl
<< "Hours Per Week: " << hours << endl;
system("pause" );
system("cls" );
}
};
int main()
{
int choice;
int idTemp;
Employee e[5];
while (1)
{
cout << "1. Enter data for an employee. " << endl
<< "2. Change data for an employee. " << endl
<< "3. Show data for an employee. " << endl
<< "4. Exit the program. " << endl
<< "Choose an option: " ;
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the ID number for an employee: " ;
cin >> idTemp;
e[idTemp].enterData(idTemp);
break ;
case 2:
cout << "Enter the ID number for an empolyee: " ;
cin >> idTemp;
e[idTemp].changeData();
break ;
case 3:
cout << "Enter the ID number for an employee: " ;
cin >> idTemp;
e[idTemp].showData();
break ;
case 4:
exit(1);
default :
cout << "Try again." << endl;
system("pause" );
break ;
}
}
cout << endl;
system("pause" );
return 0;
}
Apr 25, 2012 at 2:45am UTC
You've only provided a prototype for your Employee constructor, you need to actually define the function.
Apr 25, 2012 at 3:02am UTC
Mother of God... Can't believe I missed that.
How could I go about doing that inline? The C++ book I've got here has it done outside the class, but my professor wants us to practice using inline functions.
Apr 28, 2012 at 2:46pm UTC
Sorry, didn't see that you had replied and I totally forgot about this thread. Most (if not all) compilers will automatically inline functions for you if you define them inside that class. Otherwise, you can just prefix the definition of the function outside of the class with the
inline
keyword as in:
1 2 3 4 5 6
class x {
public :
x();
}
inline x::x() { }
Topic archived. No new replies allowed.