2011-07-19 52 views
0

我对C++非常陌生,我试图完成一些大学工作。我在Visual Studio 2010中构建我的C++程序。我已经倾注了代码,并且无法弄清楚为什么它不起作用。以下是我的项目中的所有代码。对不起,我不得不发布这一切,但我不知道问题出在哪里。C++新手 - 标题重定义错误,链接错误

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; 
} 

当我尝试调试这个程序,我得到一个消息,有生成错误。这些是我得到的构建错误。

错误1个错误LNK2019:解析外部符号 “公共:整数 __thiscall雇员::的getId(无效)”(?的getId @ @@雇员QAEHXZ)在函数引用 _main E:\ C++ 分配\ assignment_7 \ assignment_7 \ program.obj assignment_7 错误3错误LNK2019:解析外部符号 “公共:浮动 __thiscall雇员:: getBaseSalary(无效)” (getBaseSalary @ @@雇员QAEMXZ?)在函数引用_main E:\ C++ 分配\ assignment_7 \ assignment_7 \ program.obj assignment_7 错误2错误LNK2019:无法解析的外部符号“public:class std :: b asic_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) 在函数_main中引用E:\ C++ Assignments \ assignment_7 \ assignment_7 \ program.obj assignment_7 错误8错误LNK2001:无法解析的外部符号“public:virtual void __thiscall Employee :: setEmployeeDetails(void ) “ (setEmployeeDetails @ @@雇员UAEXXZ)E:?\ C++ 分配\ assignment_7 \ assignment_7 \ program.obj assignment_7 错误5错误LNK2001:解析外部符号” 公共:虚拟INT __thiscall雇员:: getNumTaskCompleted(无效) “ (?getNumTaskCompleted @ Employee @@ UAEHXZ) E:\ C++ 分配\ assignment_7 \ assignment_7 \ program.obj assignment_7 错误6错误LNK2001:解析外部符号 “公共:虚拟 浮子__thiscall雇员:: getGrossSalary(无效)” (getGrossSalary @ @@雇员UAEMXZ?)电子:\ C++ Assignments \ assignment_7 \ assignment_7 \ program.obj assignment_7 错误7错误LNK2001:无法解析的外部符号“public:virtual float __thiscall Employee :: getBonus(void)” (?getBonus @ Employee @@ UAEMXZ)E:\ C++

我完全不知道这些错误是什么意思。我查阅了错误代码,但无法从他们那里收集到任何有助于我理解我做错了什么的信息。

任何人都可以弄清楚什么是错的?谢谢!

+0

您似乎在头文件和源文件中都定义了Employee类。 – Nick

+0

还要确保所有的cpp文件实际上都在项目中(查看解决方案资源管理器窗口并确保它们在树中),而不仅仅是在编辑器中打开。如果是的话,我会预料到一些重复的定义错误。 – tinman

+0

“无法解析的外部”意味着一个实体引用源文件之外的另一个实体,并且链接程序找不到它。通常是由于没有在项目或命令行中放置所有的源代码和库。 –

回答

4

这些链接错误...的编译器编译完成你的C++代码转换成目标文件,然后连接器“链接”这些文件汇集成后可执行文件。链接器虽然说它不能找到在你的main函数中被调用的链接在一起的函数。

我现在看到的一个大问题是,你已经在你的头文件中声明了你的类,但是你在.cpp文件中重新声明它们。这会导致问题。 在您的头文件中声明您的类(正如您所做的那样),但定义了您的类在您的.cpp文件中的功能(您正在重新在您的.cpp文件中与声明一起进行声明)。

0

您不需要在头文件和源文件中都包含类声明。实施应如下所示:

employee.cpp

#include <string> 
#include <iostream> 

using namespace std; 

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

您在employee.hemployee.cpp中有class Employee的多重定义。 在employee.h中声明Employee类,并在employee.cpp中定义其方法

1

正如其他人所说的,您在不同的文件中多次声明相同的类。作为一个经验法则,为类定义使用头文件(.h扩展名)用于类别删除和源文件(.cpp扩展名)。把它们放在两个地方都是不必要的也是错误的。

0

从我的头顶...试试这个:

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,避免using namespace在头 - 这会“污染”无意识的大量代码。只有在CPP中这样做。