bool 运算符() 和继承

发布于 2024-08-21 09:00:12 字数 997 浏览 5 评论 0原文

我对派生类的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假面具 2024-08-28 09:00:12

您的 x,y,z 是私有成员,您正尝试从类外部访问它们。要么让你的points structs,或者让你的x,y,z public,或者提供setter/他们的吸气剂。

编辑:
关于您的代码的更多信息:

  1. 不要std::list 继承您的类列表,标准容器 > 旨在用作基类。如果您需要 std::container 中不可用的特殊函数,请提供一个可以执行此操作的免费函数,而不是从中继承。
  2. 考虑到这里的问题类型,实现自己的容器可能不是最好的主意。使用一些标准的,有很多它们,它们很可能会满足您的需求。
  3. 当从一个类继承另一个类时,基类通常应该是虚拟
  4. Point3D 不是某种 Point2D,它更像是 Point2DPoint3D <是一种点。对我来说,这种继承更有意义。

为了停止猜测您在那里的编译器错误,请尝试一下这段代码,我认为这大约是您正在寻找的内容。

#include <algorithm>
#include <iostream>
#include <vector>

class Point
{
public:
    Point() {}
    virtual ~Point() {}

    virtual void some_function_relevant_to_all_points() {}

private:
    // maybe some members here
};

class Point2D : public Point
{
public:
    Point2D(double x, double y)
        : x_(0),
          y_(0)
    {}
    ~Point2D() {}

private:
    double x_;
    double y_;
};

class Point3D : public Point
{
public:
    Point3D(double x, double y, double z)
        : x_(x),
          y_(y),
          z_(z)
    {}
    ~Point3D() {}

    double get_x() const {return x_;}
    double get_y() const {return y_;}
    double get_z() const {return z_;}

private:
    double x_;
    double y_;
    double z_;
};

class Compare3DPointByX
{
public:
    bool operator()(const Point3D *lhs, const Point3D *rhs) const
    {
        return lhs->get_x() < rhs->get_x();
    }
};

class DeleteElement
{
public:
    template <typename T>
    void operator()(T *arg)
    {
        delete arg;
    }
};

int main()
{
    std::vector<Point3D *> points3d;
    points3d.push_back(new Point3D(4,5,6));
    points3d.push_back(new Point3D(1,2,3));

    std::cout << "point 1:" << points3d[0]->get_x() << "\n";
    std::cout << "point 2:" << points3d[1]->get_x() << "\n";

    std::sort(points3d.begin(), points3d.end(), Compare3DPointByX());

    std::cout << "point 1:" << points3d[0]->get_x() << "\n";
    std::cout << "point 2:" << points3d[1]->get_x() << "\n";

    std::for_each(points3d.begin(), points3d.end(), DeleteElement());
    return 0;
}

您可以自己添加其余功能,此示例只是为了让您了解如何实现它。

希望它有帮助,祝你好运。

Your x,y,z are private members, and you are trying to access them from outside a class. Either make your points structs, or make your x,y,z public, or provide setters/getters for them.

EDIT:
Couple more things about your code:

  1. Do not inherit your class List from std::list, standard containers are not meant to be used as base classes. If you need a special function that's not available in std::container, provide a free function which can do that, instead of inheriting from it.
  2. Considering the type of question here, implementing your own container is probably not the best idea. Use some standard one, there's plenty of them, and they will most likely fit your needs.
  3. When inheriting one class from another, the base class should normally be virtual.
  4. Point3D isn't kind of Point2D, it's more like Point2D and Point3D 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.

#include <algorithm>
#include <iostream>
#include <vector>

class Point
{
public:
    Point() {}
    virtual ~Point() {}

    virtual void some_function_relevant_to_all_points() {}

private:
    // maybe some members here
};

class Point2D : public Point
{
public:
    Point2D(double x, double y)
        : x_(0),
          y_(0)
    {}
    ~Point2D() {}

private:
    double x_;
    double y_;
};

class Point3D : public Point
{
public:
    Point3D(double x, double y, double z)
        : x_(x),
          y_(y),
          z_(z)
    {}
    ~Point3D() {}

    double get_x() const {return x_;}
    double get_y() const {return y_;}
    double get_z() const {return z_;}

private:
    double x_;
    double y_;
    double z_;
};

class Compare3DPointByX
{
public:
    bool operator()(const Point3D *lhs, const Point3D *rhs) const
    {
        return lhs->get_x() < rhs->get_x();
    }
};

class DeleteElement
{
public:
    template <typename T>
    void operator()(T *arg)
    {
        delete arg;
    }
};

int main()
{
    std::vector<Point3D *> points3d;
    points3d.push_back(new Point3D(4,5,6));
    points3d.push_back(new Point3D(1,2,3));

    std::cout << "point 1:" << points3d[0]->get_x() << "\n";
    std::cout << "point 2:" << points3d[1]->get_x() << "\n";

    std::sort(points3d.begin(), points3d.end(), Compare3DPointByX());

    std::cout << "point 1:" << points3d[0]->get_x() << "\n";
    std::cout << "point 2:" << points3d[1]->get_x() << "\n";

    std::for_each(points3d.begin(), points3d.end(), DeleteElement());
    return 0;
}

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.

夏雨凉 2024-08-28 09:00:12
  • 类和结构定义后面应跟一个 ;
  • 标识符名称不能以数字开头
  • Classes and structure definitions should be followed by a ;
  • Identifier names cannot start with a digit
相思故 2024-08-28 09:00:12

我更正了代码(而不是检查),同样的问题......

class Point3D : public Point
{
double z;
public:
Point3D(double x, double y, double zx) : Point(x,y){z(zz);}
...
}

class compareByX
{
bool operator () (Point3D *p1, Point3D *p2) const
{
return p1->getX() < p2->getX();   //Compilation error
}
}


class List: public list<Point3D *>
{
...
}


int main()
{
List l;;
l.push_back(new Point3D(1,2,3));
l.push_back(new Point3D(4,5,6));
sort(l.begin(), l.end(), compareByX);  
}

I correct the code (thans for checks), the same question....

class Point3D : public Point
{
double z;
public:
Point3D(double x, double y, double zx) : Point(x,y){z(zz);}
...
}

class compareByX
{
bool operator () (Point3D *p1, Point3D *p2) const
{
return p1->getX() < p2->getX();   //Compilation error
}
}


class List: public list<Point3D *>
{
...
}


int main()
{
List l;;
l.push_back(new Point3D(1,2,3));
l.push_back(new Point3D(4,5,6));
sort(l.begin(), l.end(), compareByX);  
}
迷离° 2024-08-28 09:00:12

您无法像这样对 std::list 进行排序,您需要像 std::vector 这样支持随机访问的东西。或者使用 sort 成员函数:

l.sort(compareByX());  

注意我添加到 compareByX 中的括号,您需要构造一个函子,这就是括号的用途。

此外,您还必须将您的 operator() 设为公共成员函数,以便 sort 算法可以调用它。实现此目的的最简单方法是使函子成为结构:

struct compareByX
{
    bool operator () (Point3D const *p1, Point3D const *p2) const
    {
        return p1->getX() < p2->getX();   
    }
};

我怀疑您还必须将 getX 成员函数公开,但这很难说,因为您没有显示您的那部分代码。

最后,我认为这个特定示例不需要指针/堆分配。如果您在堆栈上创建点,您的程序将会更快、更健壮。

PS:请下次发布真实的代码,这将使那些试图提供帮助的人更容易。

You can't sort a std::list like that, you'll need something that supports random access like a std::vector. Either that or use the sort member function:

l.sort(compareByX());  

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 the sort algorithm can call it. The simplest way to achieve this is making your functor a struct:

struct compareByX
{
    bool operator () (Point3D const *p1, Point3D const *p2) const
    {
        return p1->getX() < p2->getX();   
    }
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文