如何使用好友类c ++ [英] How to use friend class c++
本文介绍了如何使用好友类c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有朋友点类的矩形类。我使用笛卡尔坐标,所以我将在矩形类中有四个点。点在点类中定义。当在源文件中定义矩形构造函数时,得到错误(在注释中标记):
I have a rectangle class with a friend point class. I am using cartesian coordinates, so I will have four points in the rectangle class. The points are defined in the point class. When defining the rectangle constructor in the source file, I get the error(marked in comment):
矩形没有成员Rectangle
Rectangle has no member Rectangle
标题:
using namespace std;
class Rectangle
{
public:
Rectangle(Point, Point, Point, Point);
friend class Point;
~Rectangle();
private:
Point a;
Point b;
Point c;
Point d;
};
class Point
{
public:
Point(int, int);
private:
int x;
int y;
};
来源:
Rectangle::Rectangle(Point v1, Point v2, Point v3, Point v4) //error here
{
}
Point::Point(int value1, int value2)
{
if (x <= 20 && y <= 20){
x = value1;
y = value2;
}
else{
throw invalid_argument("");
}
}
推荐答案
您的构造函数声明中的星号。
Remove the asterisks in your constructor declaration.
向前声明Point或在Rectangle之前声明Point。
Either forward declare Point, or declare Point before Rectangle.
真的不应该在头文件中使用using namespace。
You also really shouldn't use "using namespace" inside of a header file.
这篇关于如何使用好友类c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文