2012-10-06 32 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)。這些對我來說一直很難弄清楚。 –

相關問題