| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1063 人关注过本帖
标题:[求助]C++问题
只看楼主 加入收藏
ming12720
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-13
收藏
 问题点数:0 回复次数:5 
[求助]C++问题

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编辑过]

搜索更多相关主题的帖子: display include promote public 
2007-01-13 18:18
一二三四五
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:856
专家分:0
注 册:2006-11-13
收藏
得分:0 
你定义了const的变量当然是不能修改的,
你要修改它,当然会报错了,你把const去掉就可以修改了

hey,di va la
2007-01-13 22:01
ming12720
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-13
收藏
得分:0 
上楼的朋友:
把const去掉我也试过了,还是不可以
2007-01-14 09:06
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
mEmployees是一个数组,数组哪来的成员函数,把mEmployee调用成员函数的地方改成mEmployee[i]


还有,
cerr<<"no match with name"<<inFirstName<<" "<<inLastName<<endl;
throw exception();
这两句你把它们放在了循环体里面,我觉得是不是应该放在for循环的后面

2007-01-14 10:53
csusuntao
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-1-13
收藏
得分:0 
在成员函数:
void Database::displayFormer()
{
for(int i=0;i<mNextSlot;i++)
{
if(!mEmployees.getIsHired())
{
mEmployees.display();
}
}
}
中,mEmployees应该是个数组名吧,不能直接调用getIsHired()的,应该是mEmployees[i].getIsHired()吧,我是这样理解的!
而且不光在这一个函数中,在我的编译中出现的error,全部都是因为这个类型错误。 楼主可能疏忽了!
2007-01-14 13:23
ming12720
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-13
收藏
得分:0 
那样还是不行
麻烦再给看下朋友
谢了
2007-01-14 16:42
快速回复:[求助]C++问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018418 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved