Your x,y,z
are private
members, and you are trying to access them from outside a class. Either make your point
s struct
s, or make your x,y,z
public
, or provide setters/getters for them.
EDIT:
Couple more things about your code:
- 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.
- 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.
- When inheriting one class from another, the base class should normally be
virtual
.
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.