0

I am trying to write a piece of code that takes an object and pushes it onto the back of a vector, but the objects are not being pushed correctly.

I am writing an OpenGL application and I want to draw many lines in it. In order to store those lines, I first create each line in a C2DPointSet and when I stop drawing, I want to push the current line in my vector of lines. By debugging the code, I saw that the length of the vector after pushing the line is 0 and the same line in the vector has 0 points (which should not be the case). Here is the code:

vector <C2DPointSet> lines; //global variables
C2DPointSet line;

//function that runs if a button is pressed and i click in my glwindow
void lineButtonFunction(int x,int y){
    C2DPoint currentPoint;  
    currentPoint=workspacePoint(x,y);//returns point;s cordinates acording to cursorx,y-it works
    line.AddCopy(currentPoint);//adds a copy of the given point to obj line
}


//code that runs when i am done drawing 
if(buttons[i].getButtonText()=='L'){ 
    lines.push_back(line);
    line.DeleteAll();//empties the obj
}

I don't know what the problem is. With different objects the code would run. I am not sure, but i think that the problem stems from pushing C2DPointSet because it stores as many points as I like. Also, I observed that regardless of how many points I stored in the line I got its size as 12.

C2DPoints and C2DPointSet are from geolib; information can be found here.

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
Cosmo
  • 25
  • 5

2 Answers2

2

It appears that C2DPointSet has a constructor and a destructor, but no copy constructor and copy assignment operator, i.e. C2DPointSet does not follow the rule of three. This is problematic if C2DPointSet has pointer members. Does it? Then it is inherently broken. It just won't work in vectors.

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
0

Maybe it depends on the contents of C2DPointSet class. Possible that you don't have copy constructor there. My suggestion is to have a vector of references instead of vector of actual objects. That way you will also minimize the overhead. So you should have vector<&C2DPointSet> lines, instead of what you have. Try it. It might work.

santahopar
  • 2,933
  • 2
  • 29
  • 50