一个有关重载运算符"<<"问题。

LinuxPanther 2003-12-25 10:15:02
偶在帮别人 写程序时写了一个程序:可以我得重载“<<”出了错。。
帮我看看了。。
说说,注意的地方了。。。。
//student.h
// Student.h: interface for the Student class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUDENT_H__0D0FB24B_2043_4AC6_AF4D_F0E234797401__INCLUDED_)
#define AFX_STUDENT_H__0D0FB24B_2043_4AC6_AF4D_F0E234797401__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////student.h///////////////////////
#include <iostream>
#include <string>
using namespace std;
class Student
{
float score;
char *name;
char *id;
public:
Student(char *n,char *i,const float fSc);
~Student()
{
delete []name;
delete []id;
}
void ModifyScore(const float fSc);
void Output(ostream& out) const;
};

//student.cpp


#endif // !defined(AFX_STUDENT_H__0D0FB24B_2043_4AC6_AF4D_F0E234797401__INCLUDED_)
//student.cpp
// Student.cpp: implementation of the Student class.
//
//////////////////////////////////////////////////////////////////////

#include "Student.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Student::Student(char *n,char *i,const float fSc)
{
name=new char[strlen(n)+1];
strcpy(name,n);
id=new char[strlen(i)+1];
strcpy(id,i);
score=fSc;
}

inline void Student::ModifyScore(const float fSc)
{
score=fSc;
}

void Student::Output(ostream &out) const
{
out<<"ID:"<<id<<endl
<<"Name:"<<name<<endl
<<"Score:"<<score<<endl;
}

ostream& operator<<(ostream &out,const Student &x)
{
x.Output(out);
return out;
}
//main.cpp

//main.cpp
#include "student.h"
#include <iostream>
using namespace std;
void main()
{
Student s("558", "sum", 90);
s.Output(cout);
cout<<s;//问题所在
}
...全文
40 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
LinuxPanther 2003-12-27
  • 打赏
  • 举报
回复
谢谢大家的关注..
不要怪我给的分少呀...因为人太多的了..哈哈..
谢谢了..
langzi8818 2003-12-26
  • 打赏
  • 举报
回复
不知道我有没有回答道你的问题上!
langzi8818 2003-12-26
  • 打赏
  • 举报
回复
不声明友元的话,你的<<应该这样定义啊!ostream& operator<<(ostream&os)
那么你在main中cout<<student这样能行吗?不行,只有student<<cout这样才行,因为你的this是第一个参数,当你声明了友元的时候,ostream& operator<<(ostream&os,student&s)
这样才能cout<<student
sharkhuang 2003-12-26
  • 打赏
  • 举报
回复
不必是类联吧!我不访问其私有成员啊?
abitz 2003-12-26
  • 打赏
  • 举报
回复
怀疑 zxm954712(三绝剑) 就是把你写的东西都粘到一个文件里了。
这样当然不会有什么问题。但如果不在同一个文件里,估计啥编译器,
啥sp也不会好使:你必须得有个声明。
=======================
在main函数那个文件里对operator<<是不可见的。
也许在student.h里加个对operator<<的声明能行。
LinuxPanther 2003-12-26
  • 打赏
  • 举报
回复
首先,谢谢大家了。。
虽然我的问题还是没有解决。在我的心里我特别的感激大家... ....
我在vc.net下也还是哪一个问题。。。
按楼上的方法..可以了。。。
我现在在试zxm954712(三绝剑)的方法。。
happypei 2003-12-26
  • 打赏
  • 举报
回复
定义为成员函数也行吧
langzi8818 2003-12-26
  • 打赏
  • 举报
回复
回复人: abitz(阿奈) ( ) 信誉:100

我没怎么看题目,只是说说。
zxm954712 2003-12-26
  • 打赏
  • 举报
回复
sorry. I misunderstand what means. I compile all the files so it is right:)
you can add the statement 'ostream& operator<<(ostream&os,student&s);' in your main.cpp file and you can compile it.

LinuxPanther 2003-12-26
  • 打赏
  • 举报
回复
看到大家的回复,我想是应该要声明了..
我也是在声明下,解决的这个问题的了..
这个知道了..
我星期天结贴了...
abitz 2003-12-26
  • 打赏
  • 举报
回复
“当你声明了友元的时候,ostream& operator<<(ostream&os,student&s)
这样才能cout<<student”
=====================================================================
Re:是声明,不是声明为友员。
声明为友员除了使你有了访问其私有成员的权限,它和一个普通的函数声明没什么区别。
楼主的例子中根本就没有访问私有成员,何必声明为友员哪?
请注意:一个非成员的重载operator并不等价于友员函数。
murui 2003-12-25
  • 打赏
  • 举报
回复
这里一定要用友元,没有办法,这是“<<”的重载规则。
abitz 2003-12-25
  • 打赏
  • 举报
回复
楼主这种情况没必要声明为friend。
但看起来好像确个声明。至少在main函数那个文件里对operator<<是不可见的。
也许在student.h里加个对operator<<的声明能行。
/////////////////student.h///////////////////////
#include <iostream>
#include <string>
using namespace std;
class Student
{
float score;
char *name;
char *id;
public:
Student(char *n,char *i,const float fSc);
~Student()
{
delete []name;
delete []id;
}
void ModifyScore(const float fSc);
void Output(ostream& out) const;
};

ostream& operator<<(ostream &,const Student &);
==============================


还有这个:
============================
另外我觉得可不可以改一下:
ostream& operator<<(ostream &out,const Student &x)
{
x.Output(out);
return *this;
}
==============================
这个不行。楼主这种情况不适于重载为member function。
看您这return *this;象member function;
但从声明看,又是普通函数。总体看来,就是完全错误。


GameWeaver 2003-12-25
  • 打赏
  • 举报
回复
☆°★°∴°°∴ ☆°.·★°∴°.
◢◣。 ◢◣。 ☆圣★
◢★◣。 ◢★◣。 ★诞☆
◢■■◣。 ◢■■◣。 ☆节★
◢■■■◣。 ◢■■■◣。 ★快☆
︸︸||︸︸ !!︸︸||︸︸ ☆乐★
祝圣诞节快乐 ^_^!!☆°★°∴°°∴ ☆°.·★°∴°.

°★.☆° .★·°∴°★.°·∴°☆ ·°∴° ☆..·.
LinuxPanther 2003-12-25
  • 打赏
  • 举报
回复
我觉的不是这个的问题了..SP5呀..
我可是D版的呀..很难找的了.
5852 2003-12-25
  • 打赏
  • 举报
回复
以前我也是习惯的将它设为友元 不过想想非友元也是可以的 但是为什么编译会有错呢
学习

zxm954712 2003-12-25
  • 打赏
  • 举报
回复
I think you should install visual stdio server pack 5 to solve this problem.

try it :)
LinuxPanther 2003-12-25
  • 打赏
  • 举报
回复
非友员时的错误提示:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Student' (or there is no acceptable conversion)
LinuxPanther 2003-12-25
  • 打赏
  • 举报
回复
把它声明成友员就没有什么错了.
不过,我现在不想把它声明成friend ?
期待中... ....
LinuxPanther 2003-12-25
  • 打赏
  • 举报
回复
在我的机子上,的的确确是有问题得了..
不声明成友员是没有什么关系的了..
关注更多的提示... ....
加载更多回复(6)

64,647

社区成员

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

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