C++问题求助
头文件:
//Employee.h
#include<iostream>
namespace Records{
const int kDefaultStartingSalary=3000;
class Employee
{
public:
Employee();
void promote(int inRaiseAmount=1000);
void demote(int inDemeritAmount=1000);
void hire();
void fire();
void display();
//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;
};
}
//Database.h
#include<iostream>
#include"Employee.h"
namespace Records
{
const int kMaxEmployees=100;
const int kFirstEmployeeNumber=1000;
class Database
{
public:
Database();
~Database();
Employee& addEmployee(std::string inFirstName , std::string inLastName);
Employee& getEmployee(int inEmployeeNumber);
Employee& getEmployee(std::string inFirstName , std::string inLastName);
void displayAll();
void displayCurrent();
void displayFormer();
protected:
Employee mEmployees[kMaxEmployees];
int mNextSlot;
int mNextEmployeeNumber;
};
}
上面为头文件
//Datdbase.cpp
#include<iostream>
#include<stdexcept>
#include"Database.h"
using namespace std;
namespace Records
{
Database::Database()
{
mNextSlot=0;
mNextEmployeeNumber=kFirstEmployeeNumber;
}
Database::~Database()
{
}
Employee& Database::addEmployee(string inFirstName,string inLastName)
{
if(mNextSlot>=kMaxEmployees)
{
cerr<<"There is no more room to add the new employee!"<<endl;
throw exception();
}
Employee& theEmployee=mEmployees[mNextSlot++];
theEmployee.setFirstName(inFirstName);
theEmployee.setLastName(inLastName);
theEmployee.setEmployeeNumber(mNextEmployeeNumber++);
theEmployee.hire();
return theEmployee;
}
Employee& Database::getEmployee(string inFirstName,string inLastName)
{
for(int i=0;i<mNextSlot;i++)
{
if(mEmployees[i].getFirstName()==inFirstName&&
mEmployees[i].getLastName()==inLastName)
{
return mEmployees[i];
}
cerr<<"no match with name"<<inFirstName<<" "<<inLastName<<endl;
throw exception();
}
}
Employee& Database::getEmployee(int inEmployeeNumber)
{
for(int i=0;i<mNextSlot;i++)
{
if(mEmployees[i].getEmployeeNumber()==inEmployeeNumber)
{
return mEmployees[i];
}
cerr<<"No employee with employee number"<<inEmployeeNumber<<endl;
throw exception();
}
}
void Database::displayAll()
{
for(int i=0;i<mNextSlot;i++)
{
mEmployees[i].display();
}
}
void Database::displayCurrent()
{
for(int i=0;i<mNextSlot;i++)
{
if(mEmployees[i].getIsHired())
{
mEmployees[i].display();
}
}
}
void Database::displayFormer()
{
for(int i=0;i<mNextSlot;i++)
{
if(!mEmployees[i].getIsHired())
{
mEmployees[i].display();
}
}
}
}
//DatabaseTest.cpp
#include<iostream>
#include"Database.h"
using namespace Records;
using namespace std;
int main()
{
Database myDB;
Employee& emp1= myDB.addEmployee("Greg","Waills");
emp1.fire();
Employee& emp2= myDB.addEmployee("Scott","Kleper");
emp2.setSalary(3000);
Employee& emp3= myDB.addEmployee("刘","艳铭");
emp3.promote();
cout<<"all employees:"<<endl;
cout<<endl;
myDB.displayAll();
cout<<endl;
cout<<"current employees:"<<endl;
cout<<endl;
myDB.displayCurrent();
cout<< endl;
cout<<"former employees:"<<endl;
cout<<endl;
myDB.displayFormer();
}
程序不能编译 大家给改下 ,谢了
编译器: Default compiler
执行 g++.exe...
g++.exe "G:\C++程序\员工记录系统\Datdbase.cpp" -o "G:\C++程序\员工记录系统\Datdbase.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
In file included from G:\C++程序\员工记录系统\/Database.h:3,
from G:\C++程序\员工记录系统\Datdbase.cpp:89:
G:\C++程序\员工记录系统\/Employee.h:4: error: redefinition of `const int Records::kDefaultStartingSalary'
G:\C++程序\员工记录系统\/Employee.h:4: error: `const int Records::kDefaultStartingSalary' previously defined here
G:\C++程序\员工记录系统\/Employee.h:6: error: redefinition of `class Records::Employee'
G:\C++程序\员工记录系统\/Employee.h:6: error: previous definition of `class Records::Employee'
In file included from G:\C++程序\员工记录系统\Datdbase.cpp:89:
G:\C++程序\员工记录系统\/Database.h:6: error: redefinition of `const int Records::kMaxEmployees'
G:\C++程序\员工记录系统\/Database.h:6: error: `const int Records::kMaxEmployees' previously defined here
G:\C++程序\员工记录系统\/Database.h:7: error: redefinition of `const int Records::kFirstEmployeeNumber'
G:\C++程序\员工记录系统\/Database.h:7: error: `const int Records::kFirstEmployeeNumber' previously defined here
G:\C++程序\员工记录系统\/Database.h:9: error: redefinition of `class Records::Database'
G:\C++程序\员工记录系统\/Database.h:9: error: previous definition of `class Records::Database'
执行结束
[此贴子已经被作者于2007-1-14 16:33:49编辑过]