友元函数不能访问类的私有成员?
以下代码在编译时通不过,显示很多错误,我将错误信息放在代码的注示中了,请各位高手给珍断珍断,谢谢!#include <iostream>
#include <string>
using namespace std;
class String
{public:
String(){p=NULL;}
String(char *str);
friend bool operator>(String &st1,String &st2);
void display();
private: char *p;//错误2:see declaration of 'p'
};
String::String(char *str)
{p=str;}
void String::display()
{cout<<p;}
bool operator>(String &st1,String &st2)
{if(strcmp(st1.p,st2.p)>0) //错误1:error C2248: 'p' : cannot access private
// member declared in class 'String'
return true;
else return false;
}
int main()
{String st1("Hello"),st2("Book")
cout<<(st1>st2)<<endl;
return 0;
}