我是 C++ 的新手,我正在尝试完成一些大学作业。我正在 Visual Studio 2010 中构建我的 C++ 程序。我已经遍历了代码,但无法弄清楚为什么它不起作用。以下是我项目中的所有代码。抱歉,我不得不全部发布,但我不知道问题出在哪里。
员工.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
员工.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;
}
};
项目管理器.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
项目管理器.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);
}
};
软件工程师.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
软件工程师.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);
}
};
程序.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;
}
当我尝试调试这个程序时,我收到一条消息,指出存在构建错误。这些是我遇到的构建错误。
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++
我完全不知道这些错误是什么意思。我查看了错误代码,但无法从中找到任何有助于我了解我做错了什么的信息。
谁能找出问题所在?谢谢!
最佳答案
这些是链接器错误...在编译器完成将您的 C++ 代码编译成目标文件后,链接器然后将这些文件“链接”在一起成为可执行文件。链接器虽然说它找不到要链接在一起的函数,这些函数正在您的 main
函数中调用。
我现在看到的一个大问题是,您已经在头文件中声明了您的类,但随后又在 .cpp 文件中重新声明了它们。那会引起问题。在您的头文件中声明您的类(正如您所做的那样),但在您的 .cpp 文件中定义您的类函数(您在您的文件中重新声明) .cpp 文件以及定义)。
关于C++ 新手 - header 重定义错误、链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749427/