bool 运算符() 和继承
我对派生类的 bool operator() 存在以下问题 派生类的点
基
class Point
{
double x, y;
public:
Point(){x=0;y=0;}
...
}
类派生类运算符
class 3DPoint : public Point
{
double z;
public:
3DPoint(double x, double y, double zx) : Point(x,y){z(zz);}
...
}
()
class compareByX
{
bool operator () (const 3DPoint *p1, const 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
容器
class List: public list<3DPoint *>
{
...
}
int main()
{
List l;;
l.push_back(new 3DPoint(1,2,3));
l.push_back(new 3DPoint(4,5,6));
sort(l.begin(), l.end(), compareByX);
}
编译在类compareByX 中停止,并显示以下消息:无法将 3DPoint const 转换为 Point。我删除了 const 声明...
class compareByX
{
bool operator () (3DPoint *p1, 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
...并且...成功编译。但我认为operator()没有明确定义。你能帮我吗?也许提出更合适的对象模型会更好......谢谢。
I have the following problem with bool operator() for derived class
Base class
class Point
{
double x, y;
public:
Point(){x=0;y=0;}
...
}
Derived class
class 3DPoint : public Point
{
double z;
public:
3DPoint(double x, double y, double zx) : Point(x,y){z(zz);}
...
}
operator () for derived class
class compareByX
{
bool operator () (const 3DPoint *p1, const 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
Container of points
class List: public list<3DPoint *>
{
...
}
int main()
{
List l;;
l.push_back(new 3DPoint(1,2,3));
l.push_back(new 3DPoint(4,5,6));
sort(l.begin(), l.end(), compareByX);
}
The compilation stops in class compareByX with the following message: can not convert 3DPoint const to Point. I removed const declaration...
class compareByX
{
bool operator () (3DPoint *p1, 3DPoint *p2) const
{
return p1->x < p2->x; //Compilation error
}
}
... and... successful compilation. But I believe that operator () is not well defined. Can you help me, please? Perhaps it would be better to propose more suitable object model... Thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 x,y,z 是私有成员,您正尝试从类外部访问它们。要么让你的
point
sstruct
s,或者让你的x,y,z
public
,或者提供setter/他们的吸气剂。编辑:
关于您的代码的更多信息:
std::list
继承您的类列表
,标准容器不 > 旨在用作基类。如果您需要std::container
中不可用的特殊函数,请提供一个可以执行此操作的免费函数,而不是从中继承。虚拟
。Point3D
不是某种Point2D
,它更像是Point2D
和Point3D
<是一种点。对我来说,这种继承更有意义。为了停止猜测您在那里的编译器错误,请尝试一下这段代码,我认为这大约是您正在寻找的内容。
您可以自己添加其余功能,此示例只是为了让您了解如何实现它。
希望它有帮助,祝你好运。
Your
x,y,z
areprivate
members, and you are trying to access them from outside a class. Either make yourpoint
sstruct
s, or make yourx,y,z
public
, or provide setters/getters for them.EDIT:
Couple more things about your code:
class List
fromstd::list
, standard containers are not meant to be used as base classes. If you need a special function that's not available instd::container
, provide a free function which can do that, instead of inheriting from it.virtual
.Point3D
isn't kind ofPoint2D
, it's more likePoint2D
andPoint3D
are kind of Point. To me this kind of inheritance would make a bit more sense.And just to stop guessing the compiler error you have there, give a try to this code, I think it's approximately what you're looking for.
Rest of functionality you can add yourself, this example is just to give you the idea, how it might be implemented.
Hope it helps, good luck.
;
;
我更正了代码(而不是检查),同样的问题......
I correct the code (thans for checks), the same question....
您无法像这样对
std::list
进行排序,您需要像std::vector
这样支持随机访问的东西。或者使用sort
成员函数:注意我添加到
compareByX
中的括号,您需要构造一个函子,这就是括号的用途。此外,您还必须将您的
operator()
设为公共成员函数,以便sort
算法可以调用它。实现此目的的最简单方法是使函子成为结构:我怀疑您还必须将
getX
成员函数公开,但这很难说,因为您没有显示您的那部分代码。最后,我认为这个特定示例不需要指针/堆分配。如果您在堆栈上创建点,您的程序将会更快、更健壮。
PS:请下次发布真实的代码,这将使那些试图提供帮助的人更容易。
You can't sort a
std::list
like that, you'll need something that supports random access like astd::vector
. Either that or use thesort
member function:Note the parantheses I've added to
compareByX
, you need to construct a functor and that's what the parentheses are for.Also, you have to make your
operator()
a public member function so that thesort
algorithm can call it. The simplest way to achieve this is making your functor a struct:I suspect that you'll also have to make your
getX
member function public but that's difficult to tell because you're not showing that part of your code.Finally, I don't think you need pointers/heap allocation for this particular example. Your program will be faster and more robust if you create your points on the stack.
PS: please post real code next time, it will make it much easier for those trying to help.