C++友元函数的问题
今天做了个练习题,遇到问题了,请大家帮个忙。
问题入下:
// 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成员