开发者

C++ Newbie - Header Redefinition Errors, Linking Errors

开发者 https://www.devze.com 2023-03-21 03:50 出处:网络
I\'m very new to C++, I\'m trying to get some university work done. I\'m building my C++ program in Visual Studio 2010. I\'ve poured over the code, and can\'t figure out why it isn\'t working. The fol

I'm very new to C++, I'm trying to get some university work done. I'm building my C++ program in Visual Studio 2010. I've poured over the code, and can't figure out why it isn't working. The following is all the code in my project. Sorry that I had to post it all, but I don't know where the problem lies.

employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>

using namespace std;

class Employee
{
public:
    int empNumber;

    string getName();
    int getId();
    float getBaseSalary();
    virtual string getTitle();
    virtual int getNumTaskCompleted();
    virtual float getGrossSalary();
    virtual float getBonus();
    virtual void setEmployeeDetails();
};
#endif

employee.cpp

#include <string>
#include <iostream>

using namespace std;

class Employee
{
private:
    string name;
    int id;
    float salary;

public:
    string getName()
    {
        return this->name;
    }

    int getId()
    {
        return this->id;
    }

    float getBaseSalary()
    {
        return this->salary;
    }

    void setEmployeeDetails()
    {
        cout << "Enter employee name (no spaces): ";
        cin >> this->name;
        cout << "Enter employee id (numeric): ";
        cin >> this->id;
        cout << "Enter employee salary: ";
        cin >> this->salary;
    }
};

projectManager.h

#ifndef PROJECT_MANAGER_H
#define PROJECT_MANAGER_H
#include <string>
#include "employee.h"

using namespace std;

class ProjectManager : public Employee
{
public:
    string getTitle();
    int getNumTaskCompleted();
    float getGrossSalary();
    float getBonus();
    void setBonus(float bonus);
    void setEmployeeDetails();
};
#endif

projectManager.cpp

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

using namespace std;

class ProjectManager : public Employee
{
private:
    int numTaskCompleted;
    float bonus;

public:
    string getTitle()
    {
        return "Manager";
    }

    int getNumTaskCompleted()
    {
        return this->numTaskCompleted;
    }

    float getGrossSalary()
    {
        return this->getBaseSalary() + this->bonus;
    }

    float getBonus()
    {
        return this->bonus;
    }

    void setBonus(float bonus)
    {
        this->bonus = bonus;
    }

    void setEmployeeDetails()
    {
        Employee::setEmployeeDetails();

        cout << "Enter tasks completed (numeric): ";
        cin >> this->numTaskCompleted;

        if (numTaskCompleted >= 5)
            this->setBonus(10000.00f);
    }
};

softwareEngineer.h

#ifndef SOFTWARE_ENGINEER_H
#define SOFTWARE_ENGINEER_H
#include <string>
#include "employee.h"

using namespace std;

class SoftwareEngineer : public Employee
{
public:
    string getTitle();
    int getNumTaskCompleted();
    float getGrossSalary();
    float getBonus();
    void setBonus(float bonus);
    void setEmployeeDetails();
};
#endif

softwareEngineer.cpp

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

using namespace std;
class SoftwareEngineer : public Employee
{
private:
    int numTaskCompleted;
    float bonus;

public:
    string getTitle()
    {
        return "Engineer";
    }

    int getNumTaskCompleted()
    {
        return this->numTaskCompleted;
    }

    float getGrossSalary()
    {
        return this->getBaseSalary() + this->bonus;
    }

    float getBonus()
    {
        return this->bonus;
    }

    void setBonus(float bonus)
    {
        this->bonus = bonus;
    }

    void setEmployeeDetails()
    {
        Employee::setEmployeeDetails();

        cout << "Enter tasks completed (numeric): ";
        cin >> this->numTaskCompleted;

        if (numTaskCompleted >= 30)
            this->setBonus(5000.00f);
    }
};

program.cpp

#include <string>
#include <iostream>
#include <iomanip>

#include "employee.h"
#include "projectManager.h"
#include "softwareEngineer.h"

using namespace std;

Employee *_employees[4];

int main()
{
    for (int employeeIndex = 0; employeeIndex < 4; employeeIndex++)
    {
        Employee *CurrentEmployee = new Employee();
        CurrentEmployee->setEmployeeDetails();
        _employees[employeeIndex] = CurrentEmployee;
    }

    cout
        << setw(12) << "Employee" 
        << setw(5) << "Id"
        << setw(15) << "Employee"
        << setw(7) << "Task" 
        << setw(10) << "Base"
        << setw(13) << "Bonus"
        << setw(12) << "Gross" << endl;


    cout
        << setw(11) << "Title" 
        << setw(9开发者_运维知识库) << "Number" 
        << setw(10) << "Name"
        << setw(12) << "Completed"
        << setw(8) << "Salary" 
        << setw(15) << "Entitlement"
        << setw(9) << "Salary" << endl;

    for (int employeeIndex = 0; employeeIndex < 4; employeeIndex++)
    {
        cout
            << setw(12) << _employees[employeeIndex]->getTitle()
            << setw(8)  << _employees[employeeIndex]->getId()
            << setw(13) << _employees[employeeIndex]->getName()  
            << setw(5)  << _employees[employeeIndex]->getNumTaskCompleted()
            << setw(13) << _employees[employeeIndex]->getBaseSalary()
            << setw(14) << _employees[employeeIndex]->getBonus()
            << setw(11) << _employees[employeeIndex]->getGrossSalary() << endl;
    }

    return 0;
}

When I try to debug this program, I get a message that there are build errors. These are the build errors I am getting.

Error 1 error LNK2019: unresolved external symbol "public: int __thiscall Employee::getId(void)" (?getId@Employee@@QAEHXZ) referenced in function _main E:\C++ Assignments\assignment_7\assignment_7\program.obj assignment_7 Error 3 error LNK2019: unresolved external symbol "public: float __thiscall Employee::getBaseSalary(void)" (?getBaseSalary@Employee@@QAEMXZ) referenced in function _main E:\C++ Assignments\assignment_7\assignment_7\program.obj assignment_7 Error 2 error LNK2019: unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall Employee::getName(void)" (?getName@Employee@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main E:\C++ Assignments\assignment_7\assignment_7\program.obj assignment_7 Error 8 error LNK2001: unresolved external symbol "public: virtual void __thiscall Employee::setEmployeeDetails(void)" (?setEmployeeDetails@Employee@@UAEXXZ) E:\C++ Assignments\assignment_7\assignment_7\program.obj assignment_7 Error 5 error LNK2001: unresolved external symbol "public: virtual int __thiscall Employee::getNumTaskCompleted(void)" (?getNumTaskCompleted@Employee@@UAEHXZ) E:\C++ Assignments\assignment_7\assignment_7\program.obj assignment_7 Error 6 error LNK2001: unresolved external symbol "public: virtual float __thiscall Employee::getGrossSalary(void)" (?getGrossSalary@Employee@@UAEMXZ) E:\C++ Assignments\assignment_7\assignment_7\program.obj assignment_7 Error 7 error LNK2001: unresolved external symbol "public: virtual float __thiscall Employee::getBonus(void)" (?getBonus@Employee@@UAEMXZ) E:\C++

I have absolutely no idea what these errors mean. I've looked up the error codes, but couldn't gleam any information from them that would help me understand what I'm doing wrong.

Can anyone figure out what is wrong? Thanks!


These are linker errors ... after the complier finishes compiling your C++ code into object files, the linker then "links" these files together into an executable. The linker though is saying it can't find the functions to link together that are being called in your main function.

A big problem I'm seeing right now, is that you've declared your classes inside your header files, but then you are re-declaring them inside your .cpp files. That's going to cause problems. Declare your classes in your header files (as you've done), but define your class functions in your .cpp files (you're re-doing the declaration in your .cpp files along with the definition).


you are have multiple definition of class Employee in employee.h and employee.cpp. Declare class Employee in employee.h and define its methods in employee.cpp


As everyone else has said, you declared the same classes multiple times in different files. As a rule of thumb, use header files (.h extension) for class declerations and source files (.cpp extension) for definitions. Putting them in both places is both unnecessary and wrong.


You do not need to include the class declaration in both the header and source files. Implementation should look like the following:

employee.cpp

#include <string>
#include <iostream>

using namespace std;

string Employee::getName()
{
    return this->name;
}


From the top of my head... try this:

employee.h:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>

class Employee
{
private:
    std::string name;
    int id;
    float salary;

public:
    int empNumber;

    std::string getName();

    // Etc...

};
#endif

employee.cpp:

#include "employee.h"

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

// Etc...

BTW, avoid using namespace in headers - this can "pollute" unintentionally large body of code. Only do that in CPPs.

0

精彩评论

暂无评论...
验证码 换一张
取 消