Id returned 1 exit status

I was working on a problem for school to write methods for a program where main was already written, but got an error when compiling. Since im just learning any help and explanation would be helpful. Thanks.

Errors: undefined reference to `Employee::Employee(char const*, int, double)'
undefined reference to `Employee::Employee()'
undefined reference to `Employee::Employee(char const*, int, double)'
undefined reference to `Employee::Employee(char const*, int, double)'
undefined reference to `Employee::Employee(char const*, int, double)'
[Error] ld returned 1 exit status
Code:#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;
const int MIN_ID = 1000;
const int MAX_ID = 9999999;


class Employee
{
public:
Employee();
Employee( const char [], int, double );

void printEmp();
void increaseSalary( double );

void setIDnum( int );
void setSalary( double );

int getIDnum();
double getSalary();

private:
char name[25] = "None";
int idNum = 1000;
double salary = 0;
};


int main()
{

Employee e1= Employee( "Your name here", 1802054, 678367732.40 );

cout<<"The first Employee object : "<<endl;
e1.printEmp();
cout<<endl;

e1.increaseSalary(115.15);
e1.printEmp();
cout<<endl<<endl<<endl;

Employee e2;

cout<<"The second Employee object : "<<endl;
e2.printEmp();
cout<<endl;

e2.increaseSalary(-1000.00);
cout<<endl<<endl<<endl<< endl;

Employee e3= Employee("Annie Walker", 24933, 820.12);

cout<<"The third Employee object : "<<endl;
e3.printEmp();
cout<<endl;

e3.setSalary(8.12);
e3.setIDnum(654321);

e3.printEmp();
cout<<endl<<endl<<endl;

Employee e4= Employee("Eyal Lavin", 91781,2468.00);

cout<<"The fourth Employee object : "<<endl;
e4.printEmp();
cout<<endl;



e4.setIDnum(809);
cout<<endl;


e4.setSalary(9517.53);

cout<<"Employee 4 has a salary of $"<<e4.getSalary()<<endl<<endl;



e4.printEmp();
cout<<endl<<endl<<endl;


Employee e5= Employee("Auggie Anderson",2012,71940.76);

cout<<"The fifth Employee object : "<<endl<<endl;
e5.printEmp();
cout<<endl;


e5.setSalary(-1000);
cout<<endl;

cout<<"Employee 5 has an identification number of "<<e5.getIDnum()<<endl<<endl;

e5.printEmp();
cout<<endl<<endl<<endl;

return 0;
}



void Employee::printEmp(){
cout << "Employee: " << name << " ID: " << idNum << " Salary: " << salary;
}

void Employee::increaseSalary (double incrementAmount){
if (incrementAmount <= 0){
cout << "the salary cannot be increased";
}
else {
salary += incrementAmount;
}
}

void Employee::setIDnum( int newIDnum){
if (newIDnum < MIN_ID or newIDnum > MAX_ID){
cout << "Error: the new identification number is invalid. It will be set to 1000";
idNum = 1000;
}
else{
idNum = newIDnum;
}
}

void Employee::setSalary( double newSalary ){
if (newSalary <= 0){
cout << "Error: the passed in salary is invalid. The salary will be set to 0.00";
salary = 0.0;
}
else {
salary += newSalary;
}
}

int Employee::getIDnum(){
return idNum;
}

double Employee::getSalary(){
return salary;
}
The constructors are only declared, they are not defined. Ergo, undefined references to them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Employee
{
public:

    ////////// constructors defined inline ////////////////

    Employee() {}  // = default

    Employee( const char name_[], int id_, double salary_ )
    {
        if( name_ != nullptr && std::strlen(name_) < 25 ) std::strcpy( name, name_ ) ; // copy the name
        setIDnum(id_) ; // validate and set the id
        setSalary(salary_) ; // validate and set the salary
    }

    ///////////////////////////////////////////////////////
    
    // ...

};

http://coliru.stacked-crooked.com/a/d6b5c2814ab7bc99

Consider changing the type of the member 'name' to std::string
Last edited on
Topic archived. No new replies allowed.