boost::apply_visitor is not a member of [some] class
-
05-07-2021 - |
Pergunta
I have a ShapeType, Point, with some coordinates, (1,2), and I'd like to use apply_visitor in an overloaded operator () to add the coordinates (3,4) to my Point, so that Point ends up being (4,6). Where is my implementation failing? I think my ShapeVisitor class is correct, but I am getting an error, "apply_visitor" is not a member of CLARK::Point.
Code is as follows.
#include "Point_H.hpp"
#include "Shape_H.hpp"
#include "boost/variant.hpp"
typedef boost::variant<Point,Line,Circle> ShapeType;
ShapeType ShapeVariant(){...}
class ShapeVisitor : public boost::static_visitor<>
{
private:
double m_dx; // point x coord
double m_dy; // point y coord
public:
ShapeVisitor(double m_dx, double m_dy);
~ShapeVisitor();
// visit a point
void operator () (Point& p) const
{
p.X(p.X() + m_dx);
p.Y(p.Y() + m_dy);
}
};
int main()
{
using boost::variant;
ShapeType myShape = ShapeVariant(); // select a Point shape
Point myPoint(1,2);
boost::get<Point>(myShape) = myPoint; // assign the point to myShape
boost::apply_visitor(ShapeVisitor(3,4), myPoint); // trying to add (3,4) to myShape
cout << myPoint << endl;
return 0;
}
Thanks!
Solução
You are missing the include (Edit: doesn't seem to be required anymore)
#include "boost/variant/static_visitor.hpp"
Also instead of
boost::get<Point>(myShape) = myPoint;
You'll just want to do
myShape = myPoint;
Otherwise, if the variant did not actually contain a
Point
yet, you'll receive aboost::bad_get
exceptionFinally
boost::apply_visitor(ShapeVisitor(3,4), myPoint);
should have been
boost::apply_visitor(ShapeVisitor(3,4), myShape);
A simple self-contained example that shows all these points would look like this: (See it live on http://liveworkspace.org/code/33322decb5e6aa2448ad0359c3905e9d)
#include "boost/variant.hpp"
#include "boost/variant/static_visitor.hpp"
struct Point { int X,Y; };
typedef boost::variant<int,Point> ShapeType;
class ShapeVisitor : public boost::static_visitor<>
{
private:
double m_dx; // point x coord
double m_dy; // point y coord
public:
ShapeVisitor(double m_dx, double m_dy) : m_dx(m_dx), m_dy(m_dy) { }
void operator () (int& p) const { }
// visit a point
void operator () (Point& p) const
{
p.X += m_dx;
p.Y += m_dy;
}
};
int main()
{
Point myPoint{ 1,2 };
ShapeType myShape(myPoint);
boost::apply_visitor(ShapeVisitor(3,4), myShape);
myPoint = boost::get<Point>(myShape);
std::cout << myPoint.X << ", " << myPoint.Y << std::endl;
}
Output:
4, 6