C++ 不能直接调用构造函数
C++ Cannot call constructor ' ' directly
我正在编写一些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);
不是说这是个好主意,但我认为它也会起到同样的作用。
相关文章:
- 在c++中使用向量时,如何调用构造函数和析构函数
- C++:考虑但不调用构造函数的特殊性
- 对象实例化调用构造函数的次数太多
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C ++:通过大括号调用构造函数?
- 不能调用构造函数
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 在模板化类的构造函数中调用构造函数
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 我不明白在这个例子中什么时候调用构造函数
- 调用c++构造函数的不同方法
- 调用构造函数与将内联常量定义为默认参数
- 如何通过 Rust FFI 调用C++构造函数?
- "new"运算符是否总是调用构造函数?
- 无法调用构造函数
- 使用 "()" 调用构造函数不同于"{}"
- 确定是调用构造函数还是强制转换运算符的因素