0

Im just trying to create a basic class but I'm getting link error 2005.

Here is my class defintion:

Employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <string>
using namespace std;

class Employee {
private:
    string firstName;
    string lastName;
    string jobTitle;
    float baseSalary;
    float salary;
public:
    Employee(const string &, const string &,const string &, float);
    void calculateSalary(float);

    string getName() const;
    string getJobTitle() const;
    float getSalary() const;

    void print() const;

};
#endif

Employee.cpp

#include <iostream>
#include <string>

using namespace std;

#include "Employee.h"

Employee::Employee(const string &first, const string &last, const string &title, float base) {
    firstName = first;
    lastName = last;
    jobTitle = title;
    baseSalary = base;
}

void Employee::calculateSalary(float baseSalary) {
    salary = baseSalary;
}

string Employee::getName() const{
    return firstName + " " + lastName;
}

string Employee::getJobTitle() const{
    return jobTitle;
}

float Employee::getSalary() const{
    return salary;
}

void Employee::print() const {
    cout << "Name: " << firstName << " " << lastName << endl;
    cout << "Job Title: " << jobTitle << endl;
    cout << "Salary: " << salary << endl;
}

I feel like this is something obvious but cannot figure it out. I have rewrote the class multiple times.

Here are my errors:

Error   LNK2005 "public: void __thiscall Employee::calculateSalary(float)" (?calculateSalary@Employee@@QAEXM@Z) already defined in Employee.obj Lab 8 (2)   C:\Users\Zack Sloan\documents\visual studio 2017\Projects\Lab 8 (2)\Lab 8 (2)\Source.obj    1   
Error   LNK2005 "public: float __thiscall Employee::getSalary(void)const " (?getSalary@Employee@@QBEMXZ) already defined in Employee.obj    Lab 8 (2)   C:\Users\Zack Sloan\documents\visual studio 2017\Projects\Lab 8 (2)\Lab 8 (2)\Source.obj    1
Error   LNK2005 "public: void __thiscall Employee::print(void)const " (?print@Employee@@QBEXXZ) already defined in Employee.obj Lab 8 (2)   C:\Users\Zack Sloan\documents\visual studio 2017\Projects\Lab 8 (2)\Lab 8 (2)\Source.obj    1
Zack Sloan
  • 133
  • 1
  • 1
  • 8
  • 1
    What is the full error message? – Barmar Mar 31 '17 at 04:51
  • 2
    You should use the macro in pairs, the `#endif` should be in the end of `Employee.h` I think, and this can lead you to break the [One Definition Rule](https://en.wikipedia.org/wiki/One_Definition_Rule) – Jiahao Cai Mar 31 '17 at 04:52
  • @Pinky it actually is at the end of my .h file. Error when formatting the post. Will fix. – Zack Sloan Mar 31 '17 at 05:02
  • You wouldn't happen to be including Employee.cpp in source.cpp, would you? – user4581301 Mar 31 '17 at 05:12
  • @user4581301 I do include that in my source – Zack Sloan Mar 31 '17 at 05:13
  • 1
    Possible duplicate of [Why should I not include cpp files and instead use a header?](http://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header) TL;DR version: Includes effectively paste the included file into the including file, so Source.cpp compiles all of Employee.cpp. Employee.cpp is compiled on its own, so now you have two versions of the same functions in two separate files and the linker won't know which to use. Never include .cpp files. Include the .h file and let the compiler compile the .cpp. – user4581301 Mar 31 '17 at 05:46

0 Answers0