我在 OpenCV 中使用 CvPoint
结构,我需要为该结构的 x
和 y
字段赋值。
这是我的代码:
CvPoint* P1;
P2[0].x=32;
但是程序总是在尝试设置值时阻塞。
知道如何设置这些值吗?
最佳答案
首先,P1 是指向 P1 类型对象的指针。为了通过对象的指针将某些内容分配给对象的成员,您需要使用 -> 运算符。如果此指针指向数组的开头,您可以使用 operator[] 访问单个元素。此运算符返回给定索引的引用,在本例中为 CvPoint&。
<强>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。动态分配或数组
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;
由于在两个示例中都使用了 new 运算符,因此在数组的情况下,必须将它们与对 delete 或 delete[] 的匹配调用配对。
关于c++ - 如何设置 CvPoint 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7927396/