📄 subject_65475.htm
字号:
<p>
序号:65475 发表者:由零开始 发表日期:2003-12-17 21:03:41
<br>主题:请问这个友元函数为何不能访问私有成员???
<br>内容://mystring.h<BR><BR>#include <iostream><BR>using namespace std;<BR><BR>class String;<BR>istream& operator>>( istream &, String & );<BR>ostream& operator<<( ostream &, const String & );<BR><BR>class String<BR>{<BR> friend bool operator==( const String &, const String & );<BR> friend bool operator==( const char *, const String & );<BR> friend bool operator==( const String &, const char * );<BR>public:<BR> //构造和析构<BR> String( const char* = 0 );<BR> String( const String & );<BR><BR> ~String();<BR><BR> //赋值操作符的重载集合<BR> String& operator=( const String & );<BR> String& operator=( const char * );<BR> String& operator+=( const String & );<BR> String& operator+=( const char * );<BR><BR> //重载的下标操作符<BR> char& operator[]( int ) const;<BR><BR> //等于操作符的重载集合<BR> //str1 == str2;<BR> bool operator==( const char * ) const;<BR> bool operator==( const String & ) const;<BR><BR> //成员访问函数<BR> int size() { return _size; }<BR> char* c_str() { return _string; }<BR><BR>private:<BR> int _size;<BR> char *_string;<BR>};<BR><BR>///////////////////////////////////////////////////////////////////////////<BR>//string.cpp<BR><BR>#include <iostream><BR>#include "mystring.h"<BR>using namespace std;<BR><BR>inline String& String::operator += ( const String &rhs )<BR>{<BR> //如果rhs应用的String不为空<BR> if( rhs._string )<BR> {<BR> String tmp( *this );<BR><BR> //创建足够大的存储区<BR> //以便包含被连接之后的String<BR> _size += rhs._size;<BR> delete [] _string;<BR> _string = new char[ _size + 1 ];<BR><BR> //首先,把原来的String拷贝到新的存储区中<BR> //然后附加上rhs所指的String<BR> strcpy( _string, tmp._string );<BR> strcpy( _string + tmp._size, rhs._string );<BR> }<BR><BR> return *this;<BR>}<BR><BR>inline String& String::operator += ( const char *s )<BR>{<BR> //如果rhs应用的String不为空<BR> if( s )<BR> {<BR> String tmp( *this );<BR><BR> //创建足够大的存储区<BR> //以便包含被连接之后的String<BR> _size += strlen( s );<BR> delete [] _string;<BR> _string = new char[ _size + 1 ];<BR><BR> //首先,把原来的String拷贝到新的存储区中<BR> //然后附加上rhs所指的String<BR> strcpy( _string, tmp._string );<BR> strcpy( _string + tmp._size, s );<BR> }<BR><BR> return *this;<BR>}<BR><BR>bool String::operator==( const String &rhs ) const<BR>{<BR> if( _size != rhs._size )<BR> return false;<BR><BR> return strcmp( _string, rhs._string ) ? false : true;<BR>}<BR><BR><BR>/* 错误在这个函数里面!!!!!!*/<BR>bool operator==( const String& str1, const String &str2 )<BR>{<BR> if( str1._size != str2._size )<BR> return false;<BR><BR> return strcmp( str1._string, str2._string ) ? false : true;<BR>}<BR><BR>int main()<BR>{<BR> String myStr;<BR> myStr = "Sherlock";<BR><BR> return 0;<BR>}<BR><BR><BR>编译提示:<BR><BR>Compiling...<BR>string.cpp<BR>F:\LJL\CPPPRIMER\string.cpp(60) : error C2248: '_size' : cannot access private member declared in class 'String'<BR> f:\ljl\cppprimer\mystring.h(39) : see declaration of '_size'<BR>F:\LJL\CPPPRIMER\string.cpp(60) : error C2248: '_size' : cannot access private member declared in class 'String'<BR> f:\ljl\cppprimer\mystring.h(39) : see declaration of '_size'<BR>F:\LJL\CPPPRIMER\string.cpp(63) : error C2248: '_string' : cannot access private member declared in class 'String'<BR> f:\ljl\cppprimer\mystring.h(40) : see declaration of '_string'<BR>F:\LJL\CPPPRIMER\string.cpp(63) : error C2248: '_string' : cannot access private member declared in class 'String'<BR> f:\ljl\cppprimer\mystring.h(40) : see declaration of '_string'<BR>Error executing cl.exe.<BR><BR>string.obj - 4 error(s), 0 warning(s)
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Cxt_ann 回复日期:2003-12-17 21:32:16
<br>内容:问题很多:首先,你使用了strcpy等函数,却没有包含#include <cstring><BR>还有,既然你在类中声明了析构函数,不管他工作不工作,都要定义:<BR>String :: ~String()<BR>{}<BR>另外,你在类声明中声明了构造函数,为什么不定义,出错!<BR>2003-12-17 21:46:36
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -