C++重载流出错,请各位大侠帮忙,谢谢!

czh306 2001-11-19 07:37:28
/* Label.cpp
*/
#include <iostream>
#include "label.h"
using namespace std;


Label::Label():x(0),y(0)
{
s=new char(1);
*s='\0';
}

Label::Label(int X ,int Y ,const char *cText):x(X),y(Y)
{
s=new char(strlen(cText)+1);
if(s)
{
strcpy(s,cText);
}

}

Label::~Label()
{
delete s;
}

int Label::GetX(void)
{
return x;
}

int Label::GetY(void)
{
return y;
}
/* label.h
*/
#include <iostream>
#ifndef _LABEL_H_
#define _LABEL_H_
using namespace std;
class Label
{
int x;
int y;
char *s;
friend istream &operator>>(istream &is,Label &la);
friend ostream &operator<<(ostream &os,Label &la);
public:
int GetX(void);
int GetY(void);
Label();
Label(int X , int Y , const char *cText);
~Label();

};
#endif




istream &operator>>(istream &is,Label &la)
{
cout<<"enter a int X!"<<"X="<<flush;
is>>la.x;
cout<<"enter a int Y!"<<"Y="<<flush;
is>>la.y;
cout<<"enter a char text!"<<"text="<<flush;
is>>la.s;
return is;
}

ostream &operator<<(ostream &os,Label &la)
{
os<<la.x<<endl;
os<<la.y<<endl;
os<<la.s<<endl;
return os;
}
函数ostream &operator<<(ostream &os,Label &la)和
istream &operator>>(istream &is,Label &la)出错
原因:
C:\mywork\vc6code\learn\Label.cpp(42) : error C2248: 'x' : cannot access private member declared in class 'Label'
c:\mywork\vc6code\learn\label.h(9) : see declaration of 'x'
C:\mywork\vc6code\learn\Label.cpp(44) : error C2248: 'y' : cannot access private member declared in class 'Label'
c:\mywork\vc6code\learn\label.h(10) : see declaration of 'y'
C:\mywork\vc6code\learn\Label.cpp(46) : error C2248: 's' : cannot access private member declared in class 'Label'
c:\mywork\vc6code\learn\label.h(11) : see declaration of 's'
C:\mywork\vc6code\learn\Label.cpp(52) : error C2248: 'x' : cannot access private member declared in class 'Label'
c:\mywork\vc6code\learn\label.h(9) : see declaration of 'x'
C:\mywork\vc6code\learn\Label.cpp(53) : error C2248: 'y' : cannot access private member declared in class 'Label'
c:\mywork\vc6code\learn\label.h(10) : see declaration of 'y'
C:\mywork\vc6code\learn\Label.cpp(54) : error C2248: 's' : cannot access private member declared in class 'Label'
c:\mywork\vc6code\learn\label.h(11) : see declaration of 's'
Error executing cl.exe.


...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
czh306 2001-11-26
  • 打赏
  • 举报
回复
谢谢jacklondon大侠,怎么给分!
jacklondon 2001-11-21
  • 打赏
  • 举报
回复
错误不少啊,哈哈哈
1)
VC6的 STL iostream 库有一些小问题。使用 iostream.h 就可以了。将
#include <iostream>
using namespace std;
改为
#include <iostream.h>
#include <string.h>

2)圆括号改中括号的问题
Label::Label(int X ,int Y ,const char *cText):x(X),y(Y)
{
s=new char(strlen(cText)+1); ===> s=new char[strlen(cText)+1];
......
}

3)圆括号改中括号的问题
Label::Label():x(0),y(0)
{
s=new char(1); ======> s=new char[1];
}

4)不要直接用中间临时指针保存字符串。
istream &operator>>(istream &is,Label &la) istream &operator>>(istream &is,Label &la)
{ {
... char buff[255];
is>>la.s; ====> ......
... is>>buff;
} delete la.s;
la.s=new char[strlen(buff)+1];
strcpy(la.s,buff);
......
}
czh306 2001-11-19
  • 打赏
  • 举报
回复
哪位帮看一下吧!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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