2012-10-06 34 views
1

我有一个ShapeType,Point,有一些坐标,(1,2),我想在重载的运算符()中使用apply_visitor来添加坐标(3,4)到我的点,所以点最终是(4,6)。我的执行失败在哪里?我认为我的ShapeVisitor类是正确的,但我得到一个错误,“apply_visitor”不是CLARK :: Point的成员。boost :: apply_visitor不是[某些]类的成员

代码如下。

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

谢谢!

回答

3
  1. 你缺少的包括(编辑:似乎没有再被要求)

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

    boost::get<Point>(myShape) = myPoint; 
    

    你也忍不住会想做

    myShape = myPoint; 
    

    否则,如果该变种实际上并没有包含Point然而,您会收到boost::bad_get例外

  3. 最后

    boost::apply_visitor(ShapeVisitor(3,4), myPoint); 
    

    应该已经

    boost::apply_visitor(ShapeVisitor(3,4), myShape); 
    

简单显示所有这些点的独立示例如下所示: (请参阅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; 
} 

输出:

4, 6 
+0

事实上,包括 “升压/株/ static_visitor.hpp” 不改变功能。这是一个显著的变化,虽然: '提振:: apply_visitor的(ShapeVisitor(3,4),MyShape的);' 现在我得到一个错误,“错误C2664:“无效ShapeVisitor ::运算符()(CLARK: :Point&)const':无法将参数1从'T1'转换为'CLARK :: Point'' 这是ShapeVisitor成员的问题吗? –

+1

@克拉克可能,是的。在我的示例中,请注意访问者编译的'operator()(int&)const'重载_required_。简而言之,您需要一个与变体中的_every_类型相匹配的重载。你会发现为什么使用'boost :: get'的时候,当你运行已编译好的代码时,这也是一个严重错误:) – sehe

+0

我的问题是我还没有匹配变体中每种类型的重载。现在我已经创建了它们(空的,就像你的int重载运算符),并且我遇到了可怕的LNK错误! 函数_main中引用的“Test.obj:错误LNK2019:无法解析的外部符号”public:__thiscall ShapeVisitor ::〜ShapeVisitor(void)“(?? 1ShapeVisitor @@ QAE @ XZ)”和“Test.obj:error LNK2019:在函数_main“中引用的未解析的外部符号”public:__thiscall ShapeVisitor :: ShapeVisitor(double,double)“(?? 0ShapeVisitor @@ QAE @ NN @ Z)。这些对我来说一直很难弄清楚。 –

相关问题