大家帮我看看这个类的申明和定义为什么会出错

phil2360 2004-01-10 01:49:46
class Employee
{
public:
int Getage() const;
int GetYearsOfService() const;
int GetSalary() const;
void Setage(int age);
void SetYearsOfService(int year);
void SetSalary(int salary);
private:
int itsAge;
int itsYearsOfService;
int itsSalary;
};
//以上类的定义,放在一个.h文件里。


Employee::Getage() const
{
return itsAge;
}
Employee::GetSalary() const
{
return itsSalary;
}
Employee::GetYearsOfService() const
{
return itsYearsOfService;
}
Employee::Setage(int age) //从这里往下好象是错的
{
itsAge = age;
}
……
//以上类的定义,在.cpp文件里

//以下是编译器的出错信息
E:\MyVC\temp\aaa\aaa.cpp(21) : error C2556: 'int __thiscall Employee::Setage(int)' : overloaded function differs only by return type from 'void __thiscall Employee::Setage(int)'
e:\myvc\temp\aaa\nclass.h(7) : see declaration of 'Setage'
E:\MyVC\temp\aaa\aaa.cpp(21) : error C2371: 'Setage' : redefinition; different basic types
e:\myvc\temp\aaa\nclass.h(7) : see declaration of 'Setage'

编译器是Microsoft VC++6.0
...全文
42 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
phil2360 2004-01-11
  • 打赏
  • 举报
回复
谢谢各位!我已经解决了
byyyyy 2004-01-11
  • 打赏
  • 举报
回复
上面说的很对。
首先默认的返回值是int型的。比如main在c中就是int型的。可是大家一般都没有返回,因为编译器默认了。
再就是你的是void型的,当然不行了。
Wolf0403 2004-01-11
  • 打赏
  • 举报
回复
定义部分的函数都没些返回类型啊。
网际飞蚁 2004-01-10
  • 打赏
  • 举报
回复
在C++中函数的返回值默认是int,如果你在定义时没有指定函数的返回值类型系统自动按int 处理。
下面代码是等同的
Employee::Setage(int age)
{
itsAge = age;
}

int Employee::Setage(int age)
{
itsAge = age;
}
所以你在头文件中将Employee::Setage的返回值定义为void在实现中定义为int当然出错了。
davidyx 2004-01-10
  • 打赏
  • 举报
回复
对了,那个#include "check.h",是我命名的头文件,你应该包含你自己的头文件。
li190 2004-01-10
  • 打赏
  • 举报
回复
是不是成员函数定义处没有写数据类型的缘故,最好写全。
davidyx 2004-01-10
  • 打赏
  • 举报
回复
我帮你改了一些,已经通过编译。编译器是VC++6.0. 在写成员函数的实现时,要写返回值的类型

#include "check.h"

int Employee::Getage() const
{
return itsAge;
}
int Employee::GetSalary() const
{
return itsSalary;
}
int Employee::GetYearsOfService() const
{
return itsYearsOfService;
}
void Employee::Setage(int age)
{
itsAge = age;
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧