简体   繁体   English

如何设置CvPoint的值

[英]how to set values of a CvPoint

I'm using a CvPoint structure in OpenCV and I need to assign a value to x and y fields of the structure. 我在OpenCV中使用CvPoint结构,我需要为该结构的xy字段分配一个值。

Here is my code: 这是我的代码:

CvPoint* P1;
P2[0].x=32;

But the programs always block while trying to set the value. 但是程序在尝试设置该值时始终会阻塞。

Any idea about how to set these values? 关于如何设置这些值的任何想法?

Well first of all P1 is a pointer to an object of type P1. 首先,P1是指向类型为P1的对象的指针。 In order to assign something to an object's member via its pointer you need to use the -> operator. 为了通过对象的指针为对象的成员分配内容,您需要使用->运算符。 If this pointer points to the beginning of an array you use the operator[] to access individual elements. 如果此指针指向数组的开头,则可以使用operator []访问单个元素。 This operator returns a reference for the given index, in this case CvPoint& . 该运算符返回给定索引的引用,在本例中为CvPoint&

1. dynamic allocation of a single object 1.动态分配单个对象

CvPoint* P1 = new CvPoint(); // default construction of an object of type CvPoint
P1->x = 32;

// do something with P1

// clean up 
delete P1;

2. dynamic allocation or an array 2.动态分配或数组

CvPoint* points = new CvPoint[2]; // array of two CvPoints
points[0].x = 32; // operator[] returns a reference to the CvPoint at the given index
points[1].x = 32;

// do something with points

// clean up
delete[] points;

Since in both examples the new operator has been used, it is mandatory to pair them with a matching call to delete or delete[] in case of an array. 由于在两个示例中均使用了new运算符,因此必须将它们与匹配调用配对以在数组的情况下使用deletedelete []

no dynamic method: 没有动态方法:

CvPoint P1;

P1.x=32;

P1.y=32;

//////////////

CvPoint P2[2];

P2[0].x=32;

P2[0].y=32;

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

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