Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:07:30+00:00 2026-06-12T17:07:30+00:00

I have a ShapeType, Point, with some coordinates, (1,2), and I’d like to use

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:07:31+00:00Added an answer on June 12, 2026 at 5:07 pm
    1. You are missing the include (Edit: doesn’t seem to be required anymore)

      #include "boost/variant/static_visitor.hpp"
      
    2. 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 a boost::bad_get exception

    3. Finally

      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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some pages that I want to save the PRINT version of the
I have a ViewFlipper with many images. I add images to ViewFlipper like this
I have a piece of js software that is structured like so: obj =
I have a class that extends Activity and implements ViewFactory. I have found some
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have an assignment to learn how to use boost::variant. I'm trying to create
I have this xml data format. I would like to extract the properties name
I have an object: function Shape(color, position, coordinates){ this.color = color; this.position = position;
I would like to use hibernate to contain a hierarchy of objects, however the
I have a model that has several properties. The properties can be primitive (String)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.