C++ 创建一个函数的朋友?
C++ create a friend of a function?
所以我被要求为一所大学创建一个函数的朋友 class,我对实现有点迷茫。问题指出“添加函数 showCat(Cat&) 作为 Cat 的好友。
此函数应以与 Cat::showCat() 函数相同的方式显示猫的详细信息
程序如下
#include<iostream>
#include<string>
using namespace std;
class Cat
{
private:
string name;
string breed;
int age;
static constexpr double licenseFee = 10;
public:
void setCatData(string, string, int);
void showCat();
friend void showCat(const Cat& cat); //friend function declaration
};
void Cat::setCatData(string catName, string catBreed, int catAge)
{
name = catName;
breed = catBreed;
age = catAge;
}
void Cat::showCat(const Cat& aCat) //friend function definition
{
cout << "Cat: " << aCat.name << " is a " << aCat.breed << endl;
cout << "The cat's age is " << aCat.age << endl;
cout << "License fee: $" << aCat.licenseFee << endl;
}
void Cat::showCat()
{
cout << "Cat: " << name << " is a " << breed << endl;
cout << "The cat's age is " << age << endl;
cout << "License fee: $" << licenseFee << endl;
}
int main()
{
Cat myCat;
myCat.setCatData("Tigger", "Fluffy unit", 3);
myCat.showCat();
}
提供的注释并不引人注目,但这是我根据提供给我的信息所做的尝试。我对friend函数的尝试可以在猫class的public成员中的声明friend void showCat(const Cat& cat);
中看到。定义可以看作
void Cat::showCat(const Cat& aCat)
{
cout << "Cat: " << aCat.name << " is a " << aCat.breed << endl;
cout << "The cat's age is " << aCat.age << endl;
cout << "License fee: $" << aCat.licenseFee << endl;
}
这是我错误的根源。在 void Cat::showCat(const Cat& aCat)
线上,我得到
declaration is incompatible with "void Cat::showCat()" (declared at line 34)
我对朋友们的操作方式有点迷茫,所以如果能帮助我修复 function/program 将不胜感激 :)
声明
friend void showCat(const Cat& cat);
将 showCat(const Cat&)
声明为 非 成员函数。
所以你需要这样定义它:
// Note that this is not a member function
void showCat(const Cat& aCat)
{
...
}
而不是:
void Cat::showCat(const Cat& aCat) //friend function definition
做:
void showCat(const Cat& aCat) //friend function definition
void showCat(const Cat& aCat) //friend function definition
{
cout << "Cat: " << aCat.name << " is a " << aCat.breed << endl;
cout << "The cat's age is " << aCat.age << endl;
cout << "License fee: $" << aCat.licenseFee << endl;
}
可以这样定义友元函数
- 好友功能:
定义一个函数作为 class 的友元函数 :
:
friend void showCat(const Cat& cat);
然后独立实现这个功能而不是作为class的成员。
void showCat(const Cat& a)
{
cout << "Cat: " << a.name << " is a " << a.breed << endl;
cout << "The cat's age is " << a.age << endl;
cout << "License fee: $" << a.licenseFee << endl;
}
定义一个函数作为多个函数的友元class.
#include<iostream>
#include<string>
using namespace std;
class Cat
{
private:
string name;
string breed;
int age;
static constexpr double licenseFee = 10;
public:
void setCatData(string, string, int);
template<class A> friend void showAnimal(const A& a); //friend function declaration
};
class Mouse
{
private:
string name;
string breed;
int age;
static constexpr double licenseFee = 10;
public:
void setData(string, string, int);
template<class A> friend void showAnimal(const A& a); //friend function declaration
};
void Cat::setCatData(string catName, string catBreed, int catAge)
{
name = catName;
breed = catBreed;
age = catAge;
}
void Mouse::setData(string mouseName, string mouseBreed, int mouseAge)
{
name = mouseName;
breed = mouseBreed;
age = mouseAge;
}
template<class A>
void showAnimal (const A& a) //friend function definition
{
return;
}
template<>
void showAnimal <Cat>(const Cat& a) //friend function definition
{
cout << "Cat: " << a.name << " is a " << a.breed << endl;
cout << "The cat's age is " << a.age << endl;
cout << "License fee: $" << a.licenseFee << endl;
}
template<>
void showAnimal <Mouse>(const Mouse& a) //friend function definition
{
cout << "Cat: " << a.name << " is a " << a.breed << endl;
cout << "The cat's age is " << a.age << endl;
cout << "License fee: $" << a.licenseFee << endl;
}
int main()
{
Cat myCat;
myCat.setCatData("Tigger", "Fluffy unit", 3);
showAnimal(myCat);
Mouse myMouse;
myMouse.setData("Tigger", "Fluffy unit", 3);
showAnimal(myMouse);
}
将一个class定义为另一个class的朋友:
#include<iostream>
#include<string>
using namespace std;
class Cat
{
private:
string name;
string breed;
int age;
static constexpr double licenseFee = 10;
public:
void setCatData(string, string, int);
friend class Show;
};
void Cat::setCatData(string catName, string catBreed, int catAge)
{
name = catName;
breed = catBreed;
age = catAge;
}
class Show
{
public:
void showAnimal(const Cat& a)
{
cout << "Cat: " << a.name << " is a " << a.breed << endl;
cout << "The cat's age is " << a.age << endl;
cout << "License fee: $" << a.licenseFee << endl;
}
};
int main()
{
Cat myCat;
Show show;
myCat.setCatData("Tigger", "Fluffy unit", 3);
show.showAnimal(myCat);
}
定义一个class的成员函数作为另一个class的友元函数:
#include<iostream>
#include<string>
using namespace std;
class Cat;
class Show
{
public:
void showAnimal(const Cat& a);
};
class Cat
{
private:
string name;
string breed;
int age;
static constexpr double licenseFee = 10;
public:
void setCatData(string, string, int);
friend void Show::showAnimal(const Cat& a);
};
void Cat::setCatData(string catName, string catBreed, int catAge)
{
name = catName;
breed = catBreed;
age = catAge;
}
void Show::showAnimal(const Cat& a)
{
cout << "Cat: " << a.name << " is a " << a.breed << endl;
cout << "The cat's age is " << a.age << endl;
cout << "License fee: $" << a.licenseFee << endl;
}
int main()
{
Cat myCat;
Show show;
myCat.setCatData("Tigger", "Fluffy unit", 3);
show.showAnimal(myCat);
}
所以我被要求为一所大学创建一个函数的朋友 class,我对实现有点迷茫。问题指出“添加函数 showCat(Cat&) 作为 Cat 的好友。 此函数应以与 Cat::showCat() 函数相同的方式显示猫的详细信息
程序如下
#include<iostream>
#include<string>
using namespace std;
class Cat
{
private:
string name;
string breed;
int age;
static constexpr double licenseFee = 10;
public:
void setCatData(string, string, int);
void showCat();
friend void showCat(const Cat& cat); //friend function declaration
};
void Cat::setCatData(string catName, string catBreed, int catAge)
{
name = catName;
breed = catBreed;
age = catAge;
}
void Cat::showCat(const Cat& aCat) //friend function definition
{
cout << "Cat: " << aCat.name << " is a " << aCat.breed << endl;
cout << "The cat's age is " << aCat.age << endl;
cout << "License fee: $" << aCat.licenseFee << endl;
}
void Cat::showCat()
{
cout << "Cat: " << name << " is a " << breed << endl;
cout << "The cat's age is " << age << endl;
cout << "License fee: $" << licenseFee << endl;
}
int main()
{
Cat myCat;
myCat.setCatData("Tigger", "Fluffy unit", 3);
myCat.showCat();
}
提供的注释并不引人注目,但这是我根据提供给我的信息所做的尝试。我对friend函数的尝试可以在猫class的public成员中的声明friend void showCat(const Cat& cat);
中看到。定义可以看作
void Cat::showCat(const Cat& aCat)
{
cout << "Cat: " << aCat.name << " is a " << aCat.breed << endl;
cout << "The cat's age is " << aCat.age << endl;
cout << "License fee: $" << aCat.licenseFee << endl;
}
这是我错误的根源。在 void Cat::showCat(const Cat& aCat)
线上,我得到
declaration is incompatible with "void Cat::showCat()" (declared at line 34)
我对朋友们的操作方式有点迷茫,所以如果能帮助我修复 function/program 将不胜感激 :)
声明
friend void showCat(const Cat& cat);
将 showCat(const Cat&)
声明为 非 成员函数。
所以你需要这样定义它:
// Note that this is not a member function
void showCat(const Cat& aCat)
{
...
}
而不是:
void Cat::showCat(const Cat& aCat) //friend function definition
做:
void showCat(const Cat& aCat) //friend function definition
void showCat(const Cat& aCat) //friend function definition
{
cout << "Cat: " << aCat.name << " is a " << aCat.breed << endl;
cout << "The cat's age is " << aCat.age << endl;
cout << "License fee: $" << aCat.licenseFee << endl;
}
可以这样定义友元函数
- 好友功能:
定义一个函数作为 class 的友元函数 :
:
friend void showCat(const Cat& cat);
然后独立实现这个功能而不是作为class的成员。
void showCat(const Cat& a)
{
cout << "Cat: " << a.name << " is a " << a.breed << endl;
cout << "The cat's age is " << a.age << endl;
cout << "License fee: $" << a.licenseFee << endl;
}
定义一个函数作为多个函数的友元class.
#include<iostream> #include<string> using namespace std; class Cat { private: string name; string breed; int age; static constexpr double licenseFee = 10; public: void setCatData(string, string, int); template<class A> friend void showAnimal(const A& a); //friend function declaration }; class Mouse { private: string name; string breed; int age; static constexpr double licenseFee = 10; public: void setData(string, string, int); template<class A> friend void showAnimal(const A& a); //friend function declaration }; void Cat::setCatData(string catName, string catBreed, int catAge) { name = catName; breed = catBreed; age = catAge; } void Mouse::setData(string mouseName, string mouseBreed, int mouseAge) { name = mouseName; breed = mouseBreed; age = mouseAge; } template<class A> void showAnimal (const A& a) //friend function definition { return; } template<> void showAnimal <Cat>(const Cat& a) //friend function definition { cout << "Cat: " << a.name << " is a " << a.breed << endl; cout << "The cat's age is " << a.age << endl; cout << "License fee: $" << a.licenseFee << endl; } template<> void showAnimal <Mouse>(const Mouse& a) //friend function definition { cout << "Cat: " << a.name << " is a " << a.breed << endl; cout << "The cat's age is " << a.age << endl; cout << "License fee: $" << a.licenseFee << endl; } int main() { Cat myCat; myCat.setCatData("Tigger", "Fluffy unit", 3); showAnimal(myCat); Mouse myMouse; myMouse.setData("Tigger", "Fluffy unit", 3); showAnimal(myMouse); }
将一个class定义为另一个class的朋友:
#include<iostream> #include<string> using namespace std; class Cat { private: string name; string breed; int age; static constexpr double licenseFee = 10; public: void setCatData(string, string, int); friend class Show; }; void Cat::setCatData(string catName, string catBreed, int catAge) { name = catName; breed = catBreed; age = catAge; } class Show { public: void showAnimal(const Cat& a) { cout << "Cat: " << a.name << " is a " << a.breed << endl; cout << "The cat's age is " << a.age << endl; cout << "License fee: $" << a.licenseFee << endl; } }; int main() { Cat myCat; Show show; myCat.setCatData("Tigger", "Fluffy unit", 3); show.showAnimal(myCat); }
定义一个class的成员函数作为另一个class的友元函数:
#include<iostream> #include<string> using namespace std; class Cat; class Show { public: void showAnimal(const Cat& a); }; class Cat { private: string name; string breed; int age; static constexpr double licenseFee = 10; public: void setCatData(string, string, int); friend void Show::showAnimal(const Cat& a); }; void Cat::setCatData(string catName, string catBreed, int catAge) { name = catName; breed = catBreed; age = catAge; } void Show::showAnimal(const Cat& a) { cout << "Cat: " << a.name << " is a " << a.breed << endl; cout << "The cat's age is " << a.age << endl; cout << "License fee: $" << a.licenseFee << endl; } int main() { Cat myCat; Show show; myCat.setCatData("Tigger", "Fluffy unit", 3); show.showAnimal(myCat); }