【发布时间】:2016-01-21 13:10:15
【问题描述】:
对于定义类非常陌生,我遇到了在自制类中定义 VideoCapture 对象的问题。请参阅下面的代码。 我尝试创建一个包含有关视频文件的所有信息的类。所以我初始化了 videoCapture 对象。哪个工作正常,但是在“构造函数”(fName::setAviName)完成它的工作并且我调用类的另一个函数(fAvi.GetNumFrames())之后,VideoCapture 对象变成了一个 NULL 指针。 显然,当我的“构造函数”完成时,VideoCapture 对象被销毁。该类的其他私有变量工作正常。
尝试使用“共享指针”解决问题,但没有成功。
问题清楚了吗?这可能是我想做的吗?如何?还是我不应该打扰?
非常感谢,
DS
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
class fName
{
std::string d_AvifName; // name of the Avi file
std::shared_ptr<VideoCapture> d_capture;
public:
int setAviName(string const &fName); //sets name in class
int const GetNumFrames() const;
};
// functions: --------------------------------------------------
int fName::setAviName(std::string const &fName)
{ //sets AVI name in class and opens video stream
VideoCapture d_capture(fName);
if(!d_capture.isOpened()){ // check if succeeded
d_AvifName = "No AVI selected";
return (-1);
}
else{
d_AvifName = fName;
return(1);
}
}
int const fName::GetNumFrames() const
{
cout << d_capture->get(CV_CAP_PROP_FRAME_COUNT) << endl;
return d_capture->get(CV_CAP_PROP_FRAME_COUNT);
};
int main(int argc, char *argv[])
{
fName fAvi;
int IsOK = fAvi.setAviName("/Users/jvandereb/Movies/DATA/Verspringen/test_acA1300-30gc-cam5_000035.avi");
if (IsOK)
cout << fAvi.GetNumFrames() << endl;
}
【问题讨论】:
-
d_captureinsidesetAviName不引用类成员,它是一个自动变量,在函数返回时被销毁。