| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 334 人关注过本帖
标题:[讨论]友元与标准名空间矛盾问题
收藏  订阅  推荐  打印 
weishj
Rank: 2
等级:注册会员
威望:2
帖子:141
积分:1562
注册:2007-4-22
[讨论]友元与标准名空间矛盾问题

VC++6.0中存在使用了标准名空间后友元函数不能访问类私有成员的问题,但恰恰有些东西不使用标准名空间会出现问题,如果即要使用友元函数,又想使用比如string类的内容,就会出现一系列问题,下面举个例子,其代码来自解一元二次方程那个帖子,我重载了>>操作符只是为了说明这个问题,并没什么实际使用价值,其测试部分也没写
/************若这样,则编译时无法使用string类************************/
#include <iostream.h>
#include <math.h>
#include <string.h>
//using namespace std;
/*******************************************************************/
/**********************若按下面,则友元函数无法访问私有成员**********/
/*
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
*/
class funtion
{
public:
~funtion(){cout<<"求解完毕"<<endl;}
void set_value(); //输入函数
void display(); //求解函数
void show_value();//输出函数
friend istream& operator >>(istream& ins,funtion& f);
private:
float a;
float b;
float c;
float x1;
float x2;
float r;
float i;
float pd;
};
istream& operator >>(istream& ins,funtion& f)
{
string str[3];
//ax^2+bx+c=0
IfError:
ins>>f.a>>str[0]>>f.b>>str[1]>>f.c>>str[2];
if(0==a || "x^2+"!=str[0] || "x+"!=str[1] || "=0"!=str[2])
{
cerr<<"输入错误,请输入方程(ax^2+bx+c=0)"<<endl;
ins.ignore();
ins.clear();
goto IfError;
}
return ins;
}
void funtion::set_value ()
{
cout<<"输入 a b c 的值且 a b c 不可全为零"<<endl;
cin>>a;
cin>>b;
cin>>c;
}
void funtion::display ()
{
pd=b*b-4*a*c;
if(pd>0)
{
x1=-b/(2*a)+sqrt(pd)/(2*a);
x2=-b/(2*a)-sqrt(pd)/(2*a);
}
else if(pd==0)
{
x1=-b/(2*a);
x2=-b/(2*a);
}
else
{
r=-b/(2*a);
i=sqrt(-pd)/(2*a);
}
}
void funtion::show_value ()
{

if(pd>0)
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
else if(pd==0)
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
else
cout<<"x1="<<r<<'+'<<i<<"i"<<" "<<"x2="<<r<<'+'<<i<<"i"<<endl;
}
int main()
{
funtion f;
f.set_value ();
f.display ();
f.show_value ();
return 0;
}
这问题谁帮提供个解决思路,谢谢!

[此贴子已经被作者于2007-5-6 10:44:45编辑过]

搜索更多相关主题的帖子: 友元  标准名  用友  空间  string  
2007-5-5 23:19
幽灵嫖客
Rank: 1
等级:新手上路
帖子:28
积分:390
注册:2007-1-15

对了,我那个程序,编了一个友元函数也不能访问私有成员:
定义:
ostream & operator<<(ostream & os,const complex & c)
{
os<<"("<<c.real<<" , "<<c.imaginary<<"i)";
return os;
}
说什么cannot access private member declared in class 'complex',
楼主知道是怎么回事不?

C primer plus
2007-5-5 23:32
weishj
Rank: 2
等级:注册会员
威望:2
帖子:141
积分:1562
注册:2007-4-22

If you shed tears when you miss the sun, you also miss the stars.
2007-5-5 23:38
aipb2007
Rank: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18

楼主,不用更新的头文件而使友员能访问到私有成员。这个结论是个bug吧,还是编译器的。对吧?

既然这样,为什么要想办法去迎合这种巧合(可以这样说吧,毕竟那个bug非标准),有什么价值吗?

愚见而已!


Fight  to win  or  die...
2007-5-6 00:50
幽灵嫖客
Rank: 1
等级:新手上路
帖子:28
积分:390
注册:2007-1-15

也就是说只能把定义放类声明里面了!

楼上的,小弟还有个问题,我重载了一个输入操作符》,
把它的定义也是放类声明里面的,为什么有这个错误:
friend istream & operator>>(istream & is,const complex & c)
{
cout<<"real: ";
is>>c.real;
cout<<endl<<"imaginary: ";
is>>c.imaginary;
return is;
};

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const double' (or there is no acceptable conversion),
这是怎么回事?


C primer plus
2007-5-6 07:12
aipb2007
Rank: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18

楼上的,重载输入操作,右操作数怎么能是const类型呢?你要改变它的值也!

Fight  to win  or  die...
2007-5-6 09:43
weishj
Rank: 2
等级:注册会员
威望:2
帖子:141
积分:1562
注册:2007-4-22

版主说的有理,至此可不再讨论,结贴

If you shed tears when you miss the sun, you also miss the stars.
2007-5-6 10:45
幽灵嫖客
Rank: 1
等级:新手上路
帖子:28
积分:390
注册:2007-1-15

以下是引用aipb2007在2007-5-6 9:43:46的发言:
楼上的,重载输入操作,右操作数怎么能是const类型呢?你要改变它的值也!

,const用惯了,产生条件反射了!


C primer plus
2007-5-6 17:52
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.091499 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved