类的友元,怎么一直报错?
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和。
#include<iostream>
using namespace std;
class Car{
private:
char style;
int price;
char name;
int weight;
public:
char name1;
int getTotalWeight(Boat,Car);
Car()//默认构造函数
{
style='f';
price=0;
name='1';
weight=0;
}
Car(char style,int price,char name)//带参数的构造函数.
{
this->style=style;
this->price=price;
this->name=name;
weight=0;
}
~Car()//析构函数
{
cout<<"析构函数"<<endl;
}
friend int getTotalWeight(Boat boat,Car car);
};
class Boat{
private:
int price;
int length;
int weight;
public:
Boat()//默认构造函数
{
price=0;
length=0;
weight=0;
}
Boat(int price,int length,int weight)//带三个参数的构造函数.
{
this->price=price;
this->length=length;
this->weight=weight;
}
~Boat()
{
cout<<"析构函数"<<endl;
}
void setPrice(int);
void setLength(int);
int getTotalWeight(Boat boat,Car car);
};
void Boat::setLength(int length)
{
this->length=length;
}
void Boat::setPrice(int price)
{
this->price=price;
}
int Boat::getTotalWeight(Boat boat,Car car)
{
return boat.weight+Car.weight;
}
int main()
{
Boat b1;
return 0;
}
[解决办法]
友元函数
int getTotalWeight(Boat boat,Car car)
{
return boat.weight+Car.weight;
}
[解决办法]
int getTotalWeight(Boat,Car);
第11行,Boat定义,给个前向声明,然后使用指针吧
[解决办法]
1)定义为一个类的成员,另一个类的友元
2)定义为两个类的友元
#include<iostream>
using namespace std;
class Boat;
class Car{
private:
char style;
int price;
char name;
int weight;
public:
char name1;
friend int getTotalWeight(const Boat&,const Car&);
Car()//默认构造函数
{
style='f';
price=0;
name='1';
weight=0;
}
Car(char style,int price,char name)//带参数的构造函数.
{
this->style=style;
this->price=price;
this->name=name;
weight=0;
}
~Car()//析构函数
{
cout<<"析构函数"<<endl;
}//Boat boat,
};
class Boat{
private:
int price;
int length;
int weight;
public:
Boat()//默认构造函数
{
price=0;
length=0;
weight=0;
}
Boat(int price,int length,int weight)//带三个参数的构造函数.
{
this->price=price;
this->length=length;
this->weight=weight;
}
~Boat()
{
cout<<"析构函数"<<endl;
}
void setPrice(int);
void setLength(int);
friend int getTotalWeight(const Car &car);
};
void Boat::setLength(int length)
{
this->length=length;
}
void Boat::setPrice(int price)
{
this->price=price;
}
int getTotalWeight(conbst Boat&boat,const Car& car)
{
return boat.weight+car.weight;
}
int main()
{
Boat boat;
Car car;
cout<<getTotalWeight(boat,car)<<endl;;
return 0;
}
3) Boat,Car 各有一个接口 weight();
#include<iostream>
using namespace std;
class Boat;
class Car{
private:
char style;
int price;
char name;
int weight;
public:
char name1;
int getWeight()const{return weight;};
Car()//默认构造函数
{
style='f';
price=0;
name='1';
weight=0;
}
Car(char style,int price,char name)//带参数的构造函数.
{
this->style=style;
this->price=price;
this->name=name;
weight=0;
}
~Car()//析构函数
{
cout<<"析构函数"<<endl;
}//Boat boat,
};
class Boat{
private:
int price;
int length;
int weight;
public:
Boat()//默认构造函数
{
price=0;
length=0;
weight=0;
}
Boat(int price,int length,int weight)//带三个参数的构造函数.
{
this->price=price;
this->length=length;
this->weight=weight;
}
~Boat()
{
cout<<"析构函数"<<endl;
}
void setPrice(int);
void setLength(int);
int getWeight(){return weight;};
};
void Boat::setLength(int length)
{
this->length=length;
}
void Boat::setPrice(int price)
{
this->price=price;
}
int getTotalWeight(conbst Boat&boat,const Car& car)
{
return boat.getWeight()+car.getWeight();
}
int main()
{
Boat boat;
Car car;
cout<<getTotalWeight(boat,car)<<endl;;
return 0;
}
4)其实二者有共同性质,可以定义一个基类,用处理共性。
这个就不举例了。