C++ 不能直接调用构造函数

C++ Cannot call constructor ' ' directly

本文关键字:调用 构造函数 不能 C++      更新时间:2023-10-16

我正在编写一些OpenCV代码,并在windows上的VS2008中开发了它。我试图用g++在Linux上运行代码,但我收到了ImageProcessor和我创建的所有其他类的错误"无法直接调用构造函数"ImageProcessor::ImageProcessor"。我试图找到一种间接调用构造函数的方法,但没有成功。任何建议都很好。该代码在Windows上编译和运行良好。

if (x == 1){
    cout <<"MODE SELECTED: IMAGE TESTING n";
    ImageProcessor* IP = new ImageProcessor;
    LaneDetector* LD = new LaneDetector;
    LaneInfo* LI1 = new LaneInfo;
    LaneInfo* LI2 = new LaneInfo;
    LaneVector* LV = new LaneVector;
    cvNamedWindow("Window",CV_WINDOW_AUTOSIZE);
    IplImage* temp = 0;
    IplImage* img0 = 0;
    img0 = cvLoadImage(PICTURE_INPUT);
    CvRect r = cvRect(0,((img0->height)/3),img0->width,((img0->height)/3)+20);
    cout <<"IMG0 LOADED n";
    while(1){
        IP->ImageProcessor::ImageProcessor(img0, r);
        temp = IP->ImageProcessor::get_processed_image();
        LD->LaneDetector::LaneDetector(temp,r);
        LD->LaneDetector::find_edges();
        LI1 = LD->LaneDetector::find_lanes(5);
        LI2 = LD->LaneDetector::find_lanes(25);
        LV->LaneVector::LaneVector(LI1,LI2);
        LV->LaneVector::print_lane_angle_info();
        if( (cvWaitKey(20) & 255) == 27 ) break;
        cvShowImage("Window", temp);
        hold(1);
    }
}

这段代码太糟糕了。

为什么要对每个成员功能进行资格审查?

不,不能对已经创建的对象调用构造函数。初始化对象时应该提供任何构造函数参数(您的代码使用new执行此操作,这也是不好的C++编码风格)。如果这些参数应该在构造很久之后才提供,请将"构造函数"更改为具有适当名称的普通成员函数。

您的代码也有许多内存泄漏。看起来您正在用C++语法编写Java代码。这不是一件好事。

这段代码很奇怪,每次在循环中都在现有对象的顶部重建IP

不确定的语法

IP->ImageProcessor::ImageProcessor(img0, r);

一直有效。也许是在非常古老的C++中。正常的方法是

new (IP) ImageProcessor(img0, r);

不是说这是个好主意,但我认为它也会起到同样的作用。