Error LNK2005

Hello, I have been working on a simple program involving a basic class.

Whenever I compile the code below I get the following error:  main.obj : error LNK2005: "public: int __thiscall Employee::Thousand(void)const " ( Thousand@Employee@@QBEHXZ) already defined in Employee.obj



From Employee.h:
#pragma once

class Employee
{
public:
   
   
    //accessors
    int GetAge() const {return m_age;}
    void SetAge(int age) {m_age = age;}
   
    int GetService() const {return m_service;}
    void SetService(int service) {m_service = service;}
   
    int GetSalary() const {return m_salary;}
    void SetSalary(int salary) {m_salary = salary;}
   
    //member functions
    int Thousand() const;
   
private:
    int m_age, m_service, m_salary;
};

And I believe that the error may be originating from my implementation file.

From Employee.cpp

#include "Employee.h"

int Employee::Thousand() const
{
    return m_salary / 1000;
}

I am relatively new to programming and this issue has baffled me so far. Any help would be greatly appreciated.




Answer this question

Error LNK2005

  • Kevin Irl

    It looks like you are including the .cpp and still try to link to employee.obj and hence you will have 2 definitions of such function.

    You need to just include employee.h in your main cpp instead of employee.cpp.

    Let me know if that fixes the issue!

    Thanks,
      Ayman Shoukry
      VC++ Team

  • Karthik Mani

    #include <iostream>

    #include "Employee.cpp"

    using namespace std;

    int main()
    {
        Employee Josh, Ian;
        
        
        Josh.SetAge(27);
        Josh.SetSalary(42000);
        Josh.SetService(6);
        
        Ian.SetAge(23);
        Ian.SetSalary(35000);
        Ian.SetService(1);
        
        cout << "\tJosh:\n";
        cout << "Age: " << Josh.GetAge() <<endl;
        cout << "Salary: " << Josh.GetSalary() <<endl;
        cout << "Years of Service: " << Josh.GetService() <<endl;
        
        cout << "\tIan:\n";
        cout << "Age: " << Ian.GetAge() <<endl;
        cout << "Salary: " << Ian.GetSalary() <<endl;
        cout << "Years of Service: " << Ian.GetService() <<endl;
        
        
        
        cin.ignore(cin.rdbuf()->in_avail() + 1);
        return 0;
    }//end main

    Everything was working fine until I added Thousand( ) to the project.

  • llloyd

    Thanks, I had the exact same problem and this helped far more than microsoft's help (MSDN).

    I left this comment just to say thanks to those who helped put such good infomation down.


  • ryanlifferth

    It looks like you are redifinning Employee::Thousand() in your main file.

    What do you have in your main file

    Thanks,
      Ayman Shoukry
      VC++ Team



  • Tony Wang198

    Yep, that clears it up. Thanks a bunch.

  • rhombus

    A good habit to get into to avoid problems like that is to always put something in the header file to ensure it only gets included once.  In this case something like this:
    ...
    // beginning of employee.h
    #ifndef _EMPLOYEE_H
    #define _EMPLOYEE_H

    // header file code

    // end of employee.h
    #endif // _EMPLOYEE_H

    Then you don't have to deal with that problem anymore.  '#pragma once' is great if you are using a C compiler, but I've never seen it do anything in C++.

  • Error LNK2005