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 124715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:02:39+00:00 2026-05-11T05:02:39+00:00

Last question for tonight, I promise. These pointers are giving me a serious headache.

  • 0

Last question for tonight, I promise. These pointers are giving me a serious headache.

I have a std::list<Point> called Polygon and a std::list of Polygons defined like:

typedef std::list<Point> Polygon;  typedef std::list<Polygon> PolygonList;   // List of all our polygons  PolygonList polygonList;  

I created the method below to attempt to delete the nearest Point from an (x,y), checking all of my Polygons within my polygonList.

void deleteNearestPoint(int x, int y) {     y = screenHeight - y;      Polygon &closestPolygon = polygonList.front();     Polygon::iterator closestPoint = closestPolygon.begin();      float closestDistance = sqrt(pow(x - closestPoint->x, 2) + pow(y - closestPoint->y, 2));      // Search PolygonList     PolygonList::iterator listIter;     Polygon::iterator iter;      for(listIter = polygonList.begin(); listIter != polygonList.end(); listIter++)     {         Polygon &tempPolygon = *listIter;          for(iter = tempPolygon.begin(); iter != tempPolygon.end(); iter++)         {             const float distance = sqrt(pow(x - iter->x, 2) + pow(y - iter->y, 2));              if (distance < closestDistance)             {                 closestPolygon = *listIter;                 closestPoint = iter;                 closestDistance = distance;             }         }      }      closestPolygon.erase(closestPoint);      redraw(); } 

However, somewhere I have a pointer or reference variable screwing me up. This code compiles but acts in a very strange way.

I’ve written a debug statement and lets say I have 3 polygons in my polygon list like so:

Polygon #: 0
Point: (448, 43)
Point: (469, 177)
Point: (374, 123)
Polygon #: 1
Point: (295, 360)
Point: (422, 350)
Point: (315, 266)
Point: (295, 360)
Polygon #: 2
Point: (143, 202)
Point: (301, 203)
Point: (222, 100)
Point: (143, 202)

Now, lets say I try and use the delete function giving it an x/y close to point 422, 350 The desired result would be it simply deleting that point (422, 350) from Polygon #1 but instead I get this:

Polygon #: 0
Point: (295, 360)
Point: (422, 350)
Point: (315, 266)
Point: (295, 360)
Polygon #: 1
Point: (295, 360)
Point: (315, 266)
Point: (295, 360)
Polygon #: 2
Point: (143, 202)
Point: (301, 203)
Point: (222, 100)
Point: (143, 202)

It did delete (422, 350) but it also has the strange side effect of overwriting Polygon#0 to what Polygon#1 was before the delete of its point.

I know I’m using a pointer or reference incorrectly in my method. Can someone point out what I could possibly be doing that is causing this? I think it is because my &closestPolygon is declared as a reference, but I get compile errors if I try to set it as anything else.

  • 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. 2026-05-11T05:02:40+00:00Added an answer on May 11, 2026 at 5:02 am

    Other answers have pointed out what caused the error. As a general advice I would suggest not using references except in function arguments. The semantics are confusing, also for someone that will try to read your code. Try rewriting to something like this (I didn’t test the code):

    void deleteNearestPoint(int x, int y) {     y = screenHeight - y;      PolygonList::iterator closestPolygon = polygonList.begin();     Polygon::iterator closestPoint = closestPolygon->begin();      float closestDistance = sqrt(pow(x - closestPoint->x, 2) + pow(y - closestPoint->y, 2));      // Search PolygonList     PolygonList::iterator listIter;     Polygon::iterator iter;      for(listIter = polygonList.begin(); listIter != polygonList.end(); listIter++)     {         for(iter = listIter->begin(); iter != listIter->end(); iter++)         {             const float distance = sqrt(pow(x - iter->x, 2) + pow(y - iter->y, 2));              if (distance < closestDistance)             {                 closestPolygon = listIter;                 closestPoint = iter;                 closestDistance = distance;             }         }      }      closestPolygon->erase(closestPoint);      redraw(); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My last question was adding two consecutive numbers in a list . But answers
my last question on pointer cursors. in Html you have a input file tag.
Similar to my last question, but I ran into problem lets say I have
building on my last question jQuery getting values from multiple selects together I have
This should be my last question on Jquery Sortable...for a while :) I have
This will be my last question for the evening and a while. I have
I almost have an answer to my last question , but I need help.
From my last question , I have new idea for database protection. The following
in my last question i thought i have problem with the XML but there
I have updated this question(found last question not clear, if you want to refer

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.