问题
I'm working on some OpenCV code and developed it in VS 2008 on windows. I'm trying to run the code on Linux with g++ but I get the error "Cannot call constructor 'ImageProcessor::ImageProcessor' directly" for ImageProcessor and all of the other classes I have created. I've attempted to find a way to indirectly call the constructor, but to no avail. Any suggestion would be great. The code compiles and runs fine on 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);
}
}
回答1:
This code is terrible.
Why are you qualifying every member function?
And no, you can't call a constructor on an already-created object. Any constructor parameters should be provided when you initialize the object (which your code does with new
, which is also not good C++ coding style). If these arguments aren't supposed to be provided until long after construction, change the "constructor" into a normal member function with an appropriate name.
Your code has numerous memory leaks also. It looks like you're writing Java code with C++ syntax. That's not a good thing.
回答2:
This code is pretty weird, reconstructing IP
each time through the loop, on top of an existing object?
Not sure the syntax
IP->ImageProcessor::ImageProcessor(img0, r);
was ever valid. Maybe in very old C++. The normal way of doing this is
new (IP) ImageProcessor(img0, r);
Not saying it's a good idea, but I think it'll do the same thing.
来源:https://stackoverflow.com/questions/9253619/c-cannot-call-constructor-directly