C++重载流出错,请各位大侠帮忙,谢谢!
/* 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.