[英]C++ Error: IntilliSense identifier “track” is undefined — “potentially uninitialized local pointer variable 'track'”
I'm trying to build a library of cvblob using OpenCV, and while compiling in VS 2013, 我正在尝试使用OpenCV构建cvblob库,并在VS 2013中进行编译时,
error C4703: potentially uninitialized local pointer variable 'track' used
error C4703: potentially uninitialized local pointer variable 'blob' used
I'm not sure why this is, since the pointer variables are defined a block above (albeit in a separate loop). 我不确定为什么会这样,因为指针变量是在上面的一个块中定义的(尽管在单独的循环中)。 Here is the code:
这是代码:
// Update track
//cout << "Matching: track=" << track->id << ", blob=" << blob->label << endl;
track->label = blob->label; // ERROR HERE
track->centroid = blob->centroid;
I'm using pre-written header and source files, so I'm not sure what the problem is. 我正在使用预写的头文件和源文件,所以我不确定是什么问题。 Anyone know what the fix is?
有人知道解决办法吗?
Before //Update Track
, Here is where 'track' and 'blob' are referenced above, with no errors: //Update Track
之前,这是上面引用“ track”和“ blob”的位置,没有错误:
// Select track
CvTrack *track;
unsigned int area = 0;
for (list<CvTrack*>::const_iterator it=tt.begin(); it!=tt.end(); ++it)
{
CvTrack *t = *it;
unsigned int a = (t->maxx-t->minx)*(t->maxy-t->miny);
if (a>area)
{
area = a;
track = t;
}
}
// Select blob
CvBlob *blob;
area = 0;
If the tt
list is empty, or if it doesn't contain an element with the required area, then track
will never be initialized. 如果
tt
列表为空,或者如果它不包含具有所需区域的元素,则track
不会被初始化。 This is what the compiler complains about. 这就是编译器所抱怨的。
The function should probably only try to update the rack if it was actually found in the list of tracks. 如果实际上在轨道列表中找到了机架,则该功能可能应该仅尝试更新机架。
The situation for blob
is probably similar. blob
的情况可能相似。
You should avoid declaring pointers without giving them a value. 您应该避免在不给指针赋值的情况下声明它们。 Hence you should replace the declaration with
因此,您应该将声明替换为
CvTrack *track=NULL; (if you use C++ without the latest version) or
CvTrack *track=nullptr; (with C++11)
With this you're sure you will always give a value to your pointer (even if it's NULL). 使用此方法,您可以确保始终为指针提供一个值(即使它为NULL)。 Then you should check before assigning anything if the pointer is NULL to avoid a runtime error.
然后,应在分配任何内容之前检查指针是否为NULL,以避免运行时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.