#include<iostream>
#include<string>
using namespace std;
class phonebook
{
char name[20];
int areacode;
int perfix;
int num;
public:
phonebook(char *n,int a,int p,int nm)
{
strcpy(name,n);
areacode=a;
perfix=p;
num=nm;
}
friend ostream &operator<<(ostream &stream,phonebook o) ;
};
ostream &operator<<(ostream &stream,phonebook o)
{
stream<<o.name<<" ";
stream<<"("<<o.areacode<<")"<<endl;
stream<<o.perfix<<"-"<<o.num<<endl;
return stream;
}
void main()
{
phonebook a("Ted",111,555,1234);
cout<<a;
}
问题描述:这个是我抄书的,重载<<作为类的友元函数写在了类成员函数里,但在编译时出现不能访问私友成员的提示如下:
Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(23) : error C2248: 'name' : cannot access private member declared in class 'phonebook'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(7) : see declaration of 'name'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(24) : error C2248: 'areacode' : cannot access private member declared in class 'phonebook'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(8) : see declaration of 'areacode'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(25) : error C2248: 'perfix' : cannot access private member declared in class 'phonebook'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(9) : see declaration of 'perfix'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(25) : error C2248: 'num' : cannot access private member declared in class 'phonebook'
D:\Program Files\Microsoft Visual Studio\MyProjects\my4\my4.cpp(10) : see declaration of 'num'
请帮我看一下是那个地方出现了问题,谢了啊
!!