我对C ++很陌生,我正在努力完成一些大学工作。我正在Visual Studio 2010中构建我的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:未解析的外部符号“public:int __thiscall Employee :: getId(void)“(?getId @ Employee @@ QAEHXZ)引用 在函数_main E:\ C ++中 Assignments \ assignment_7 \ assignment_7 \ program.obj assignment_7 错误3错误LNK2019:未解析的外部符号“public:float __thiscall Employee :: getBaseSalary(void)“ (?getBaseSalary @ Employee @@ QAEMXZ)在函数_main E:\ C ++中引用 Assignments \ assignment_7 \ assignment_7 \ program.obj assignment_7 错误2错误LNK2019:未解析的外部符号“public:class 的std :: basic_string的,类 std :: allocator&gt; __thiscall Employee :: getName(void)“ (?的getName @ @@雇员QAE?AV?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ 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 @ Employee @@ UAEXXZ)E:\ C ++ Assignments \ assignment_7 \ assignment_7 \ program.obj assignment_7 错误5错误LNK2001:未解析的外部符号“public:virtual int __thiscall Employee :: getNumTaskCompleted(void)“ (?getNumTaskCompleted @ Employee @@ UAEHXZ)E:\ C ++ Assignments \ assignment_7 \ assignment_7 \ program.obj assignment_7 错误6错误LNK2001:未解析的外部符号“public:virtual float __thiscall Employee :: getGrossSalary(void)“ (?getGrossSalary @ Employee @@ UAEMXZ)E:\ 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 :(得分:4)
这些是链接器错误...在编译器完成将C ++代码编译成目标文件之后,链接器然后将这些文件“链接”到一个可执行文件中。链接器虽然说它无法找到在main
函数中调用的链接在一起的函数。
我现在看到的一个大问题是,你已经在头文件中声明了你的类,但是你在你的.cpp文件中重新声明了它们。这会引起问题。 在您的标题文件中声明您的类(就像您已经完成的那样),但定义您的.cpp文件中的类函数(您正在重新执行您的声明) .cpp文件以及定义)。
答案 1 :(得分:1)
class Employee
和employee.h
中有employee.cpp
的多个定义。
在employee.h中声明类Employee并在employee.cpp中定义其方法
答案 2 :(得分:1)
正如其他人所说,你在不同的文件中多次声明了相同的类。根据经验,对类定义使用头文件(.h扩展名),对定义使用源文件(.cpp扩展名)。将它们放在两个地方都是不必要的和错误的。
答案 3 :(得分:0)
您不需要在头文件和源文件中包含类声明。实施应如下所示:
employee.cpp
#include <string>
#include <iostream>
using namespace std;
string Employee::getName()
{
return this->name;
}
答案 4 :(得分: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...
顺便说一下,在标题中避免使用using namespace
- 这会“无意中”污染大量代码。只在CPPs中这样做。