VC6不支持"friend"?????

donaldlu 2003-01-03 07:10:41
一段非常简单的代码,在VC6下无法通过总说“无法访问私有成员”(大概是这个意思)。但在.NET下可以通过。
#include<iostream>

using namespace std;

class String{
public:
friend ostream& operator<<(ostream& out,String& s);
String(const char* str);
private:
char* str;
};

String::String(const char* s)
{
str=new char[strlen(s)+1];
memcpy(str,s,strlen(s)+1);
}

ostream& operator<<(ostream& out,String & s)
{
cout<<s.str;
return out;
}

void main()
{
String s("hello");
cout<<s;
}
为什么?哪位碰上过相似的问题?
...全文
43 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenqing1128 2003-04-15
  • 打赏
  • 举报
回复
奇怪的是
我的现在可以了
去这看看
http://expert.csdn.net/Expert/topic/1661/1661544.xml?temp=.5240137
peterretep 2003-04-15
  • 打赏
  • 举报
回复
把SP5装上就可以了
chenqing1128 2003-04-15
  • 打赏
  • 举报
回复
改正:cout应改为out(笔误)
请问大家上述问题是怎么回事啊
chenqing1128 2003-04-15
  • 打赏
  • 举报
回复
我也是的
在vc下实现不了friend,说不能读取私有成员
如果照上面phiger(phiger)说的用
ostream& operator<<(ostream& out,String & s)
{
cout<<s.GetStr();
return out;
}
那个getstr()本来就是公有的,当然直接可以用了
我干吗还要用友元啊。
用友元就是为了直接读取私有的啊

In355Hz(好象一条狗)说编译没问题
不会吧
我的怎么也不可以啊
突击召唤师 2003-01-04
  • 打赏
  • 举报
回复
必须使用sp5,这是一个bug
shitalone 2003-01-04
  • 打赏
  • 举报
回复
同意将cout改为out
ilmself0451 2003-01-04
  • 打赏
  • 举报
回复
一定可以支持的啊!
呵,自己写了一个类的重载友元函数,竟不用!还要用取类的成员吗?何况成员是私有的!
楼上兄弟的两种实现方法都是可行的!
liweifeng81 2003-01-03
  • 打赏
  • 举报
回复
......
dreamlivecn 2003-01-03
  • 打赏
  • 举报
回复
ostream& operator<<(ostream& out,String & s)
{
cout<<s.str;//这里不能使用cout,因为这是对“<<”的重载,
return out;
}
fingerfox 2003-01-03
  • 打赏
  • 举报
回复
agree In355Hz(好象一条狗)
rebuild it ???
In355Hz 2003-01-03
  • 打赏
  • 举报
回复
我在 VC6 下试了一下,变异和结果都没有问题,??

#include <string.h>
#include <iostream.h>

class String{
public:
friend ostream& operator<<(ostream& out,String& s);
String(const char* str);
private:
char* str;
};

String::String(const char* s)
{
str=new char[strlen(s)+1];
memcpy(str,s,strlen(s)+1);
}

ostream& operator<<(ostream& out,String & s)
{
out<<s.str;
return out;
}

void main()
{
String s("hello");
cout<<s;
}
Landlord 2003-01-03
  • 打赏
  • 举报
回复
ostream& operator<<(ostream& out,String & s)
{
cout<<s.str;//把这里的cout改成out就可以了
return out;
}
phiger 2003-01-03
  • 打赏
  • 举报
回复
for example:
#include<iostream>

using namespace std;

class String{
public:
friend ostream& operator<<(ostream& out, String& s);
String(const char* str);
char * GetStr() { return str; }
private:
char* str;
};

String::String(const char* s)
{
str=new char[strlen(s)+1];
memcpy(str,s,strlen(s)+1);
}

ostream& operator<<(ostream& out,String & s)
{
cout<<s.GetStr();
return out;
}

void main()
{
String s("hello");
cout << s;
}
omale 2003-01-03
  • 打赏
  • 举报
回复
当然支持了。
phiger 2003-01-03
  • 打赏
  • 举报
回复
define a public method to get the str member!!
donaldlu 2003-01-03
  • 打赏
  • 举报
回复
听不懂...难道你的意思是
cout<<str;
return out;
???
这样更不行了。
phiger 2003-01-03
  • 打赏
  • 举报
回复
It maybe incorrect in statement: cout<<s.str,the str is private member, although operator<< is friend,but it only can access str,not the s.str!

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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