标题:[求助]C++中关于类方法的简单调用错误
只看楼主
wz_wxz
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-3
 问题点数:0 回复次数:6 
[求助]C++中关于类方法的简单调用错误

jH3hUs9L.txt (687 Bytes) [求助]C++中关于类方法的简单调用错误

程序如下:(可以见附件) #include<iostream.h> class employee { public: int getage() const; void setage(int age); int getyearsofservice()const; void setyearsofservice(int years); int getsalary() const; void setsalary(int salary); private: int age; int yearsofservice; int salary; }; int main() { employee john; employee sally; john.setage(30); john.setyearsofservice(5); john.setsalary(50000); sally.setage(32); sally.setyearsofservice(8); sally.setsalary(40000); cout<<"john is"<<john.getage<<"years old and he has been with"; cout<<"the firm for"<<john.getyearsofservice<<"year .\n"; cout<<"john earns $"<<john.getsalary()<<"dollars per year. \n\n"; return 0; } 为什么编译时报这种错误: Compiling... 61.cpp C:\myvc\61\61.cpp(26) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion) C:\myvc\61\61.cpp(27) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion) Error executing cl.exe.

61.exe - 2 error(s), 0 warning(s)

搜索更多相关主题的帖子: int sally employee john 
2005-06-03 23:23
wz_wxz
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-3
得分:0 
为什么没人回答呢?先顶下!
2005-06-04 18:00
shan183
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-5-30
得分:0 

我帮你搞定了,主要原因是你的头文件包含的原因。 另外,建议你一开始养成良好的编程风格和编程习惯。 //首先,建立类的定义头文件employee.h #ifndef H_employee #define H_employee

#include<iostream>

using namespace std;

class employee { public: void setage(int ages); void setyearsofservice(int years); void setsalary(int salarys); int getage() const; int getyearsofservice()const; int getsalary() const; private: int age; int yearsofservice; int salary; };

#endif

//然后,建立类的实现文件employeeImp.cpp #include "employee.h" #include <iostream>

using namespace std; void employee::setage(int ages) { age=ages; }

void employee::setyearsofservice(int years) { yearsofservice=years; }

void employee::setsalary(int salarys) { salary=salarys; }

int employee::getage() const { return age; }

int employee::getyearsofservice()const { return yearsofservice; }

int employee::getsalary() const { return salary; } //最后,编写你的主程序mainProgram.cpp #include "employee.h" #include <iostream>

using namespace std;

int main() { employee john; employee sally;

john.setage(30); john.setyearsofservice(5); john.setsalary(50000);

sally.setage(32); sally.setyearsofservice(8); sally.setsalary(40000);

cout<<"john is "<<john.getage()<<" years old and he has been with "; cout<<"the firm for "<<john.getyearsofservice()<<" year."<<endl; cout<<"john earns $"<<john.getsalary()<<" dollars per year."<<endl;

return 0; } 现在运行,没有任何问题! 运行结果: john is 30 years old and he has been with the firm for 5 year. john earns $50000 dollars per year.

[此贴子已经被作者于2005-6-4 20:12:25编辑过]

2005-06-04 20:04
wz_wxz
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-3
得分:0 
谢谢你的帮助和建议。我忘记加类的实现部分了。
2005-06-05 21:43
rl
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2005-4-2
得分:0 
前面的
#ifndef H_employee
#define H_employee
是干什么用的??
2005-06-06 16:57
shan183
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-5-30
得分:0 
#ifndef H_employee #define H_employee ... ... #endif 这就是所谓的“包含警卫”(including guard),可以防止同一个头文件被重复包含。
2005-06-08 15:33
program
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-6-13
得分:0 
#ifndef H_employee #define H_employee ... ... #endif 请教:这些预处理命令在什么情况下才使用啊?
2005-06-13 21:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-20041-1-1.html




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

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