我对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++
我完全不知道这些错误是什么意思。我查阅了错误代码,但无法从他们那里收集到任何有助于我理解我做错了什么的信息。
任何人都可以弄清楚什么是错的?谢谢!
您似乎在头文件和源文件中都定义了Employee类。 – Nick
还要确保所有的cpp文件实际上都在项目中(查看解决方案资源管理器窗口并确保它们在树中),而不仅仅是在编辑器中打开。如果是的话,我会预料到一些重复的定义错误。 – tinman
“无法解析的外部”意味着一个实体引用源文件之外的另一个实体,并且链接程序找不到它。通常是由于没有在项目或命令行中放置所有的源代码和库。 –