none
Error LNK2005 RRS feed

  • Question

  • 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.


    Friday, December 9, 2005 8:44 PM

Answers

  • 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
    Friday, December 9, 2005 10:28 PM
    Moderator
  • 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++.
    Friday, December 9, 2005 10:50 PM

All replies

  • 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

    Friday, December 9, 2005 9:08 PM
    Moderator
  • #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.
    Friday, December 9, 2005 10:15 PM
  • 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
    Friday, December 9, 2005 10:28 PM
    Moderator
  • 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++.
    Friday, December 9, 2005 10:50 PM
  • Yep, that clears it up. Thanks a bunch.
    Saturday, December 10, 2005 12:19 AM
  • 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.

    Sunday, January 15, 2006 11:16 PM