C++ boost::apply_访问者不是[some]类的成员

C++ boost::apply_访问者不是[some]类的成员,c++,boost,operator-overloading,visitor,boost-variant,C++,Boost,Operator Overloading,Visitor,Boost Variant,我有一个形状类型Point,带有一些坐标1,2,我想在重载操作符中使用apply_visitor将坐标3,4添加到我的点上,因此该点最终是4,6。我的实现失败在哪里?我认为我的ShapeVisitor类是正确的,但我得到一个错误,apply_visitor不是CLARK::Point的成员 代码如下 #include "Point_H.hpp" #include "Shape_H.hpp" #include "boost/variant.hpp" typedef boost::variant&

我有一个形状类型Point,带有一些坐标1,2,我想在重载操作符中使用apply_visitor将坐标3,4添加到我的点上,因此该点最终是4,6。我的实现失败在哪里?我认为我的ShapeVisitor类是正确的,但我得到一个错误,apply_visitor不是CLARK::Point的成员

代码如下

#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;
}
谢谢

您缺少“包含编辑”:似乎不再需要了

#include "boost/variant/static_visitor.hpp"
也代替

boost::get<Point>(myShape) = myPoint;
否则,如果变量实际上还没有包含点,您将收到boost::bad\u get异常

最后

boost::apply_visitor(ShapeVisitor(3,4), myPoint);
应该是

boost::apply_visitor(ShapeVisitor(3,4), myShape);
显示所有这些要点的简单自包含示例如下所示: 看现场直播


事实上,包括boost/variant/static_visitor.hpp不会改变功能。不过,这是一个重大的变化:boost::apply_visitorShapeVisitor3,4,myShape;现在我得到一个错误,错误C2664:“void ShapeVisitor::operator CLARK::Point&const”:无法将参数1从“T1”转换为“CLARK::Point&”,这是ShapeVisitor成员的问题吗?@CLARK可能是的。在我的示例中,请注意operatorint&const重载是访问者编译所必需的。简而言之,您需要一个重载来匹配变量中的每种类型。您将发现为什么在运行编译后的代码时使用boost::get也是非常错误的:我的问题是,我还没有一个重载匹配变量中的每种类型。现在我已经创建了空的,就像你的int重载操作符一样,我遇到了可怕的LNK错误!Test.obj:错误LNK2019:未解析的外部符号公共:u\u此调用ShapeVisitor::~ShapeVisitorvoid??1 ShapeVisitor@@QAE@XZ在函数_main和Test.obj中引用:错误LNK2019:未解析的外部符号公共:_\u此调用ShapeVisitor::ShapeVisitor双精度,双精度??0 ShapeVisitor@@QAE@NN@Z在函数_main中引用。我一直很难弄明白这些问题。@Clark再次看到我的示例:您需要实现在ShapeVisitor中声明的函数。如果确实如此,请参阅以获取帮助,在那里找到解决方案。其实没那么复杂。你很快就会明白的,这就是我的意思。特别是,我添加了:void操作符Line&lp const{}和这个void操作符Circle&cp const{}。这些是我唯一的其他形状。这让我更进一步,但我遇到了这个新的链接器错误。我现在正在检查你提供的线程。
boost::apply_visitor(ShapeVisitor(3,4), myShape);
#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;
}
4, 6