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;
}
谢谢!
事实上,包括 “升压/株/ static_visitor.hpp” 不改变功能。这是一个显著的变化,虽然: '提振:: apply_visitor的(ShapeVisitor(3,4),MyShape的);' 现在我得到一个错误,“错误C2664:“无效ShapeVisitor ::运算符()(CLARK: :Point&)const':无法将参数1从'T1'转换为'CLARK :: Point'' 这是ShapeVisitor成员的问题吗? –
@克拉克可能,是的。在我的示例中,请注意访问者编译的'operator()(int&)const'重载_required_。简而言之,您需要一个与变体中的_every_类型相匹配的重载。你会发现为什么使用'boost :: get'的时候,当你运行已编译好的代码时,这也是一个严重错误:) – sehe
我的问题是我还没有匹配变体中每种类型的重载。现在我已经创建了它们(空的,就像你的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)。这些对我来说一直很难弄清楚。 –