注册 登录
编程论坛 C++教室

为什么友员函数报错不能访问私有成员???

aipb2007 发布于 2007-04-21 14:59, 2080 次点击

class add
{
public:
add(){}; //构造函数1
add(const char [100]); //构造函数2
void show(); //输出私有字符串变量a
friend add operator + (const add &,const add &); //友员重载+
private:
char a[100]; //
};

//两个构造函数
add::add(const char s[100])
{strcpy(a,s);}

void add::show()
{cout<<'\"'<<a<<'\"';}


//两个友员函数
add operator + (const add &a,const add &b)
{
add temp(a.a);
strcat(temp.a,b.a);
return temp;
}

为什么友员函数报错不能访问私有成员???

VC++6.0中!

13 回复
#2
Arcticanimal2007-04-21 16:39

为什么我试了一下却可以访问呢?
#include <iostream.h>
class T
{
public:
T(char c):bit(c)
{}
friend T operator+(T& a,T& b)
{
T temp(a.bit); //这里可以
return T((a.bit+b.bit)%127);
}
friend ostream& operator<<(ostream& os, T& a)
{
os<<a.bit; //这里也可以
return os;
}
private:
char bit;

};
int main()
{
T a('a');
T b('b');
cout<<a+b<<endl;
return 0;
}
两个程序应该是一样的

#3
aipb20072007-04-21 16:50

你把声明和定义分开试试!

#4
fat_june2007-04-21 17:08
回复:(aipb2007)为什么友员函数报错不能访问私有成...

呵呵~这个程序其实是我写的……现在修改了一下

虽然写得不好,但是应该能运行才对啊……但是就是说:cannot access private member declared in class 'add'

不知道怎样解决了

//友员函数实现字符串连接

#include <iostream>
#include <string>
using namespace std;

//类add
class add
{
public:
add(){} //构造函数1
add(char s[100]) {strcpy(a,s);} //构造函数2
void show() {cout<<'\"'<<a<<'\"';} //输出私有字符串变量a
friend add operator + (add &,add &); //友员重载+
private:
char a[100];
};


//友员函数
add operator + (add &a,add &b)
{
/*//这个也不行
add temp=a;
strcat(temp.a,b.a);
return temp;
*/
strcat(a.a,b.a);
return a;
}


/****************** main ******************/
int main()
{
//input
char s[100]={0};
cout<<"Input the first string:\t\t";
cin>>s;
add ad1(s);
cout<<"Input the secontd string:\t";
cin>>s;
add ad2(s);

//add
add sum=ad1+ad2; //调用重载函数

//output
cout<<endl;
ad1.show();
cout<<'+';
ad2.show();
cout<<'=';
sum.show();
cout<<endl;

return 0;
}

#5
Arcticanimal2007-04-21 17:20

仍然可以
//////////////test.h////////////////
#include <iostream.h>
class T
{
public:
T(char c):bit(c)
{}
friend T operator+(T& ,T& );
friend ostream& operator<<(ostream& os, T& a)
{
os<<a.bit; //这里也可以
return os;
}
private:
char bit;

};
//////////////a.h//////////////////
#include "test.h"
T operator+(T& a,T& b)
{
T temp(a.bit);
return T((a.bit+b.bit)%127);
}
///////////////main//////////////
#include "a.h"
int main()
{
T a('a');
T b('b');
cout<<a+b<<endl;
return 0;
}

#6
aipb20072007-04-21 17:23
楼上的用的VC++6.0?

我在VC++6.0上编译时报错

你再改下,都写在一个文件里,只是把友元重载写在类外面。

呵呵
#7
游乐园2007-04-21 18:57
aipb2007 你的VC++6.0 没有打sp6补丁吧 ...

在类定义的前面 加上几个声明试试

class T;
T operator+(T& ,T& );
ostream& operator<<(ostream&, T&);
#8
fat_june2007-04-21 19:55
真不明白~~~~~~
在DEV-CPP里没问题的,VC6.0就出现问题了

后来我把using namespace std; 注释掉,头文件加上".h",就可以了……

不知道什么原因,编译器版本问题?
#9
aipb20072007-04-21 20:02
以下是引用游乐园在2007-4-21 18:57:50的发言:
aipb2007 你的VC++6.0 没有打sp6补丁吧 ...

在类定义的前面 加上几个声明试试

class T;
T operator+(T& ,T& );
ostream& operator<<(ostream&, T&);

我记的我安装的那个VC就是sp6啊!

在哪里有SP6的补丁?大哥给我个撒!

#10
游乐园2007-04-21 20:17
  我也没有 ...现在都是集成sp6的VC++了  ,单独的不再提供下载了 连微软官方网站上也提供到sp5
#11
yuyunliuhen2007-04-21 22:05
还是用VC++2005,或DEV C++吧
#12
wfpb2007-04-21 22:30
我这里就是在官方下载的sp6,但是不知道怎么给你们
#13
aipb20072007-04-21 22:53
以下是引用wfpb在2007-4-21 22:30:51的发言:
我这里就是在官方下载的sp6,但是不知道怎么给你们

给个地址就OK了!

#14
weishj2007-04-23 19:06

VC++6.0下,只有用形如#include <iostream.h>的形式包含头文件并且去掉using namespace std;这一句后友元函数才能访问私有成员,这据说是VC++6.0的一个BUG,但具体还不能确定.

1