0

My professor gave me two class header and .cpp files to build on. When I include these in main, they work fine. Whenever I just use his files, I get linker errors with clang and xcode.

Here's the error:

shannigan@mbp-007100 inheritance (master) $ make main
c++     main.cpp   -o main
Undefined symbols for architecture x86_64:
  "SavitchEmployees::SalariedEmployee::SalariedEmployee()", referenced from:
      _main in main-0d7e27.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

Here's my main:

#include "employee.h"
#include "salariedemployee.h"
#include <string>
#include <cstdlib>
#include <iostream>

using namespace SavitchEmployees;
using namespace std;

    int main() {
        cout << "Do I run?" << endl;
        SalariedEmployee sam;
        return 0;
    };

The header file for Employee:

//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
using std::string;

namespace SavitchEmployees
{

    class Employee
    {
    public:
        Employee( );
        Employee(const string& theName, const string& theSsn);
        string getName( ) const;
        string getSsn( ) const;
        double getNetPay( ) const;
        void setName(const string& newName);
        void setSsn(const string& newSsn);
        void setNetPay(double newNetPay);
        void printCheck( ) const;
    protected:
        string name;
        string ssn;
        double netPay;
    };

}//SavitchEmployees

#endif //EMPLOYEE_H

The CPP file for main:

//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h.
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::string;
using std::cout;

namespace SavitchEmployees
{
    Employee::Employee( ) : name("No name yet"), ssn("No number yet"), netPay(0)
    {
        //deliberately empty
    }

    Employee::Employee(const string& theName, const string& theNumber)
       : name(theName), ssn(theNumber), netPay(0)
    {
        //deliberately empty
    }

    string Employee::getName( ) const
    {
        return name;
    }

    string Employee::getSsn( ) const
    {
        return ssn;
    }

    double Employee::getNetPay( ) const
    {
        return netPay;
    }

   void Employee::setName(const string& newName)
    {
        name = newName;
    }

    void Employee::setSsn(const string& newSsn)
    {
        ssn = newSsn;
    }

    void Employee::setNetPay (double newNetPay)
    {
        netPay = newNetPay;
    }

    void Employee::printCheck( ) const
    {
        cout << "\nERROR: printCheck FUNCTION CALLED FOR AN \n"
             << "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
             << "Check with the author of the program about this bug.\n";
        exit(1);
    }

}//SavitchEmployees

SalariedEmployees header:

//This is the header file salariedemployee.h.
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H

#include <string>
#include "employee.h"

using std::string;

namespace SavitchEmployees
{

    class SalariedEmployee : public Employee
    {
    protected:
        double salary;//weekly

    public:
        SalariedEmployee( );
        SalariedEmployee (const string&  theName, const string&  theSsn,
                                  double theWeeklySalary);
        double getSalary( ) const;
        void setSalary(double newSalary);
        void printCheck( );
    };

}//SavitchEmployees

#endif //SALARIEDEMPLOYEE_H

SalariedEmployee.cpp:

//This is the file salariedemployee.cpp
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the header file salariedemployee.h.
#include <iostream>
#include <string>
#include "salariedemployee.h"
using std::string;
using std::cout;
using std::endl;

namespace SavitchEmployees
{
    SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
    {
        //deliberately empty
    }

    SalariedEmployee::SalariedEmployee(const string&  newName, const string&  newNumber,
                                  double newWeeklyPay)
                     : Employee(newName, newNumber), salary(newWeeklyPay)
    {
        //deliberately empty
    }

    double SalariedEmployee::getSalary( ) const
    {
        return salary;
    }

    void SalariedEmployee::setSalary(double newSalary)
    {
        salary = newSalary;
    }

    void SalariedEmployee::printCheck( )
    {
        setNetPay(salary);
        cout << "\n__________________________________________________\n";
        cout << "Pay to the order of " << getName( ) << endl;
        cout << "The sum of " << getNetPay( ) << " Dollars\n";
        cout << "_________________________________________________\n";
        cout << "Check Stub NOT NEGOTIABLE \n";
        cout << "Employee Number: " << getSsn( ) << endl;
        cout << "Salaried Employee. Regular Pay: "
             << salary << endl;
        cout << "_________________________________________________\n";
    }
}//SavitchEmployees

How can I get rid of these linker errors so I can focus on my actual code? Is there anything obvious wrong? The only thing I've changed was making the "private" variables protected.

Sara
  • 1
  • 2
  • 1
    Maybe you just forget to put it here, but i must ask, where is the class `SalariedEmployee` implemented? – Petok Lorand Oct 02 '18 at 05:52
  • I updated this with SalariedEmployee class. :) I forgot. Sorry! – Sara Oct 02 '18 at 05:55
  • Can not reproduce the error, but i compiled with all the code in a single file. One more question, did you put all the header files and source files in the same location as your main.cpp? – Petok Lorand Oct 02 '18 at 06:02
  • Yes, both in xcode and in the actual folder with main. – Sara Oct 02 '18 at 06:18

1 Answers1

0

I can't see the class named SalariedEmployee. I think the main function should look like this.

int main() {
    cout << "Do I run?" << endl;
    Employee sam;
    return 0;
};

You have to use Employee instead of SalariedEmployee

dnlkng
  • 829
  • 1
  • 10
  • 16
  • I actually forgot the SalariedEmployee class - I've added it now. One note, it doesn't work with just the employee either. – Sara Oct 02 '18 at 05:56
  • Are all files located in the same directory? It seems the linker can't find the object files for employee. How have you added the employee files to your project? – dnlkng Oct 02 '18 at 06:03