简体   繁体   English

将对象推送到矢量中

[英]Issue pushing objects into a vector

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. 我正在编写一个OpenGL应用程序,我想在其中绘制许多行。 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. 为了存储这些行,我首先在C2DPointSet创建每一行,当我停止绘制时,我想在我的矢量行中推送当前行。 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). 通过调试代码,我看到推动行后向量的长度为0,向量中的同一行有0个点(不应该是这种情况)。 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. 我不确定,但我认为问题源于推送C2DPointSet因为它存储了我喜欢的点数。 Also, I observed that regardless of how many points I stored in the line I got its size as 12. 此外,我观察到无论我在线中存储了多少点,我的大小都是12。

C2DPoints and C2DPointSet are from geolib; C2DPointsC2DPointSet来自geolib; information can be found here . 信息可以在这里找到。

It appears that C2DPointSet has a constructor and a destructor, but no copy constructor and copy assignment operator, ie C2DPointSet does not follow the rule of three . 看起来C2DPointSet有一个构造函数和一个析构函数,但没有复制构造函数和复制赋值运算符,即C2DPointSet不遵循三个规则 This is problematic if C2DPointSet has pointer members. 如果C2DPointSet具有指针成员,则这是有问题的。 Does it? 可以? Then it is inherently broken. 然后它本身就被打破了。 It just won't work in vectors. 它只是在向量中不起作用。

Maybe it depends on the contents of C2DPointSet class. 也许它取决于C2DPointSet类的内容。 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. 所以你应该有vector <&C2DPointSet>行,而不是你拥有的行。 Try it. 试试吧。 It might work. 它可能会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM