Click here to Skip to main content
15,352,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am writing an app. in c++ that manages a list of polygons

Each polygon contains a list of points

The point is defined as follows:

typedef struct
{
float x;
float y;
float z;
}PolygonPoint;


The Polygon is defined as follows:

typedef struct
{
int id;
char name[max_path];
std::list< PolygonPoint > PointsList;
}PolygonData;

and in my program i define a list of polygons:
std::list< PolygonData > Polygons;

I declare a polygon:

PolygonData stPol;

When i try to populate stPol list of points i get a "list insert iterator outside range" error

I cant access stPol.PointsList for some reason.

Is there an initialization call that I should perform before using the list?

I am not declaring any variable as a pointer

Thanks

dj4400

What I have tried:

declaring a points list and populating it manually and then assigning it to the
stPol.PointsList:

std::list < PolygonPoint > points;

PolygonPoint currP;
currP.x = 1;
currP.y = 2;
currP.z = 3;
points.push_back(currP)

//assignment
stPol.PointsList = points;
Posted
Updated 22-May-22 1:46am
Comments
0x01AA 22-May-22 7:21am
   
I just assembled that and ran it on an online c++ environment without any problem.
Maybe you should post your exact code that we do not need to speculate.

I just ran this code and it worked OK:
C++
std::list< PolygonData > Polygons;

//I declare a polygon:

PolygonData stPol;
stPol.id = 1;
strcpy(stPol.name, "Foo");

PolygonPoint ppt;
ppt.x = 1;
ppt.y = 1;
ppt.z = 1;
stPol.PointsList.push_back(ppt);

PolygonPoint pptb;
pptb.x = 1;
pptb.y = 1;
pptb.z = 1;
stPol.PointsList.push_back(pptb);

Polygons.push_back(stPol);

Maybe you could show all your code in a single block, and please add proper <pre> tags around it, as I have done here.

[edit]
I just noticed that you have:
C++
//assignment
stPol.PointsList = points;

But PointsList is already declared as a std::list so you just need to add each structure to it thus:
C++
stPol.PointsList.push_back(currP);

[/edit]
   
v2
Same here, ran the code without problems on https://www.onlinegdb.com/online_c++_compiler[^]

C++
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <iostream>
#include <list>
using namespace std;
typedef struct
{
    float x;
    float y;
    float z;
}PolygonPoint;

typedef struct
{
    int id;
    char name[100];
    std::list< PolygonPoint > PointsList;
}PolygonData;


int main()
{
    cout<<"Test start\r\n";
    std::list< PolygonData > Polygons;
    PolygonData stPol;
    
    std::list < PolygonPoint > points;

    PolygonPoint currP;
    currP.x = 1;
    currP.y = 2;
    currP.z = 3;
    points.push_back(currP);

    //assignment
    stPol.PointsList = points;

    cout<<"Test end\r\n";

    return 0;
}
   
v2
Comments
dj4400 22-May-22 7:59am
   
Thank you all for your answers

I'm sorry it was my mistake:

I called memset for stPol in order to initialize it to 0 and this
harmed the list inside (- stPol.PointsList)
Thanks again
0x01AA 22-May-22 8:10am
   
You are very welcome.
To initialize the structs you can write a constructor for PolygonPoint and PolygonData
Richard MacCutchan 22-May-22 7:59am
   
Yes, it worked for me also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900