链接器错误 - 对“namespace :: class :: function()”的未定义引用

时间:2014-10-09 17:51:10

标签: c++

我正在从wrox

的Professional C ++学习C ++

以下是简单员工记录程序的3个文件

1.Employee.h

// Employee.h
#include <iostream>
namespace Records {
const int kDefaultStartingSalary = 30000;
class Employee
{
public:
Employee();
void promote(int inRaiseAmount = 1000);
void demote(int inDemeritAmount = 1000);
void hire(); // Hires or rehires the employee
void fire(); // Dismisses the employee
void display(); // Outputs employee info to the console
// Accessors and setters
void setFirstName(std::string inFirstName);
std::string getFirstName();
void setLastName(std::string inLastName);
std::string getLastName();
void setEmployeeNumber(int inEmployeeNumber);
int getEmployeeNumber();
void setSalary(int inNewSalary);
int getSalary();
bool getIsHired();
private:
std::string mFirstName;
std::string mLastName;
int mEmployeeNumber;
int mSalary;
bool fHired;
};
}

2.Employee.cpp

// Employee.cpp
#include <iostream>
#include "Employee.h"
using namespace std;
namespace Records {
Employee::Employee()
{
mFirstName = "";
mLastName = "";
mEmployeeNumber = -1;
mSalary = kDefaultStartingSalary;
fHired = false;
}
void Employee::promote(int inRaiseAmount)
{
setSalary(getSalary() + inRaiseAmount);
}
void Employee::demote(int inDemeritAmount)
{
setSalary(getSalary() - inDemeritAmount);
}
void Employee::hire()
{
fHired = true;
}
void Employee::fire()
{
fHired = false;
}
void Employee::display()
{
cout << "Employee: " << getLastName() << ", " << getFirstName() << endl;
cout << "-------------------------" << endl;
cout << (fHired ? "Current Employee" : "Former Employee") << endl;
cout << "Employee Number: " << getEmployeeNumber() << endl;
cout << "Salary: $" << getSalary() << endl;
cout << endl;
}
// Accessors and setters
void Employee::setFirstName(string inFirstName)
{
mFirstName = inFirstName;
}
string Employee::getFirstName()
{
return mFirstName;
}
void Employee::setLastName(string inLastName)
{
mLastName = inLastName;
}
string Employee::getLastName()
{
return mLastName;
}
void Employee::setEmployeeNumber(int inEmployeeNumber)
{
mEmployeeNumber = inEmployeeNumber;
}
int Employee::getEmployeeNumber()
{
return mEmployeeNumber;
}
void Employee::setSalary(int inSalary)
{
mSalary = inSalary;
}
int Employee::getSalary()
{
return mSalary;
}
bool Employee::getIsHired()
{
return fHired;
}
}

3.EmployeeTest.cpp

// EmployeeTest.cpp
#include <iostream>
#include <conio.h>
#include "Employee.h"
using namespace Records;
int main (int argc, char** argv)
{
std::cout <<"Testing the Employee class." << std::endl;
Employee emp;
emp.setFirstName("Marni");
emp.setLastName("Kleper");
emp.setEmployeeNumber(71);
emp.setSalary(50000);
emp.promote();
emp.promote(50);
emp.hire();
emp.display();
getch();
return 0;
}

当我编译EmployeeTest.cpp时,我得到一个链接器

&#34; [链接器错误]未定义对`Records :: Employee :: Employee()&#34;

的引用

我知道它有一个简单的答案,但我无法解决这个问题。 任何帮助高度赞赏。

1 个答案:

答案 0 :(得分:1)

您需要一次编译两个源文件或编译为目标文件并立即链接它们。

E.g。

$ gcc Employee.cpp EmployeeTest.cpp