1.OutputSeg.h
#ifndef OUTPUTSEG_H
#define OUTPUTSEG_H
namespace ORB_SLAM2
{
extern int img_width;
extern int img_height;
}
#endif
extern int img_width
和 extern int img_height是
对 img_width
和 img_height
这两个变量的声明。使用 extern
关键字表示这些变量是在其他地方定义的,而不是在当前文件中定义。这样可以在当前文件中使用这些变量,而不需要重新定义它们。
2.trt_infer.cpp
#include "OutputSeg.h"
namespace ORB_SLAM2
{
int img_width;
int img_height;
void Segment::Run()
{
img_width = src.cols;
img_height = src.rows;
}
}
在这里,int img_width和int img_height属于变量的定义,变量的定义只能在一个文件中进行,以避免重复定义的错误。
此外,此变量的定义必须在ORB_SLAM2的命名空间(相同的命名空间)下,在此处不能在void Segment::Run()函数里定义,否则OutputSeg.h中的声明会找不到位置。
3.ORBextractor.cc
#include "OutputSeg.h"
namespace ORB_SLAM2
{
void ORBextractor::CheckMovingKeyPoints()
{
search_coord.x=(img_width -1);
search_coord.y=(img_height -1) ;
}
}
由于引用了OutputSeg.h文件,相当于在此文件也进行了全局变量的声明,因此直接调用这个全局变量即可。注:因为在trt_infer.cpp文件中已经对全局变量进行了定义,因此在此处不能在进行定义,避免出现重复定义的问题。