C++友元函数的问题

hemacn 2011-10-24 06:05:47
今天做了个练习题,遇到问题了,请大家帮个忙。
问题入下:
// 01回溯.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
template<class Typew,class Typep>
class Knap
{
public:
friend Typep Knapsack(Typep p[],Typew w[],Typew c,int n);
friend class Object;
//private:
Typep Bound(int i);
void Backstrack(int i);
Typew c;
int n;

Typew *w;
Typep *p;
Typew cw;
Typep cp;
Typep bestp;

};


template <class Typew,class Typep>
void Knap<Typew,Typep>::Backstrack(int i)
{
if(i>n){
bestp=cp;
return;
}
if(cw+w[i]<=c)
{
cw+=w[i];
cp+=p[i];

Backstrack(i+1);
cw-=w[i];
cp-=p[i];


}
if(Bound(i+1)>bestp)
Backstrack(i+1);
}
template<class Typew,class Typep>
Typep Knap<Typew,Typep>::Bound(int i)
{
Typew cleft=c-cw;
Typep b=cp;
while(i<=n&&w[i]<=cleft)
{
cleft-=w[i];
b+=p[i];
i++;

}
if(i<=n)b+=p[i]*cleft/w[i];
return b;
}
class Object
{

public:

int operator<=(Object a)const
{return (d>=a.d);}
Object operator=(Object &a);
friend int Knapsack(int*,int *,int,int);
friend void QuickSort(Object a[],int p,int r);
friend int Partition(Object a[],int p,int r);
friend class Knap<int,int>;


private:
int ID;
float d;
};
Object Object::operator=(Object &a)
{
ID=a.ID;
d=a.d;
return *this;
}

void QuickSort(Object a[],int p,int r)
{
if(p<r)
{ int q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}

int Partition(Object a[],int p,int r)
{
int i,j;Object x;
float y;
i=p;j=r+1;
x=a[p];y=a[i].d;
while(true)
{
while(a[++i].d<y&&i<r);
while(a[--j].d>y);
if(i>=j)break;
Object temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;

}
a[p]=a[j];
a[j]=x;
return j;

}
template<class Typew ,class Typep>
Typep Knapsack(Typep p[],Typew w[],Typew c,int n)
{
Typew W=0;
Typep P=0;
Object *Q=new Object[n];
for(int i=1;i<=n;i++)
{
Q[i-1].ID=i;
Q[i-1].d=1.0*p[i]/w[i];
P+=p[i];
W+=w[i];
}
if(W<=c)return P;
QuickSort(Q,0,n-1);
Knap<Typew,Typep>K;
K.p=new Typep[n+1];
K.w=new Typew[n+1];
for(int i=1;i<=n;i++)
{
K.p[i]=p[Q[i-1].ID];
K.w[i]=w[Q[i-1].ID];
}
K.cp=0;
K.cw=0;
K.c=c;
K.n=n;
K.bestp=0;


K.Backstrack(1);
delete []Q;
delete []K.w;
delete []K.p;
return K.bestp;
}
int _tmain(int argc, _TCHAR* argv[])
{ int n,c,i=1,m,j,k=1,f;
cout<<"输入被报个数:"<<endl;
cin>>n;
j=n;
f=n;
cout<<"输入背包容量:"<<endl;
cin>>c;
int *p=new int[n+1];
int *w=new int[n+1];

p[0]=0;
w[0]=0;
cout<<"输入价值数组:"<<endl;
while(n>0)
{ cin>>m;
p[i++]=m;
// w[i++]=j;
n--;
}
cout<<"输入重量数组:"<<endl;
while(j>0)
{ cin>>m;
w[k++]=m;
j--;
}
/*int n=4,c=7;
int p[5]={0,9,10,7,4};
int w[5]={0,3,5,2,1};*/
for(int i=1;i<=f;i++)
{ if(i==1)
cout<<"价值数组为:"<<setw(3)<<p[i];
else
cout<<setw(3)<<p[i];
}
cout<<endl;
for(int i=1;i<=f;i++)
{if(i==1)
cout<<"重量数组为:"<<setw(3)<<w[i];
else
cout<<setw(3)<<w[i];
}
cout<<endl;
//cout<<p[2];
//

int x=Knapsack<int,int>(p,w,c,f);
//
cout<<"最大价值为"<<x<<endl;
delete []p;
delete []w;


return 0;
}


出错信息为:
错误 2 error C2248: “Object::d”: 无法访问 private 成员(在“Object”类中声明) d:\vc\01回溯\01回溯\01回溯.cpp 130
错误 1 error C2248: “Object::ID”: 无法访问 private 成员(在“Object”类中声明) d:\vc\01回溯\01回溯\01回溯.cpp 129
错误 4 error C2248: “Object::ID”: 无法访问 private 成员(在“Object”类中声明) d:\vc\01回溯\01回溯\01回溯.cpp 141
错误 5 error C2248: “Object::ID”: 无法访问 private 成员(在“Object”类中声明) d:\vc\01回溯\01回溯\01回溯.cpp 142
警告 3 warning C4244: “=”: 从“double”转换到“float”,可能丢失数据 d:\vc\01回溯\01回溯\01回溯.cpp 130


为什么已经把 Knapsack()函数声明为Object的友元了 还是任然无法访问Object的private成员
...全文
195 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
饿半肚 2011-12-31
  • 打赏
  • 举报
回复
friend int Knapsack(int*,int *,int,int);

这样的写法,是把普通函数“int Knapsack(int*,int *,int,int);”声明为类的友元了,而不是把模板函数“template<class Typew ,class Typep> Typep Knapsack(Typep p[],Typew w[],Typew c,int n)”声明为友元。

可以把那句改为——

template<class Typew ,class Typep>
friend Typep Knapsack(Typep p[],Typew w[],Typew c,int n);

又或者,假如你只想把两种模板类型都为int的模板函数特化声明为友元,则可以这样写——

friend int Knapsack<int, int>(int p[],int w[],int c,int n);
無_1024 2011-10-24
  • 打赏
  • 举报
回复
建议不要直接修改似有成员 还是用函数吧 封装性好一点
yaningfan 2011-10-24
  • 打赏
  • 举报
回复
....那你在你的CObject类中的私有变量前也加上friend标识符

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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