1

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);
    }
}
Wade G
  • 105
  • 3
  • 3
  • 8
  • Please supply the declaration for `ImageProcessor` – Ed Heal Feb 12 '12 at 23:17
  • You need to tell us where the error occurs and like Ed says, what does ImageProcessor look like? The first line in the while loop looks strange to me. – John3136 Feb 12 '12 at 23:22
  • 3
    C++ works differently than that. Start with a good book. – Kerrek SB Feb 12 '12 at 23:23
  • ¤ In C++ variables don't have to be `new`-ed unless you want an arbitrary lifetime. Just declare a variable `v` of type `T` as `T v;`. For your image processor object, declare that variable *within the loop*, like `ImageProcessor ip( img0, r );`. That's all re the stated problem. But in addition you should refrain from using ALL UPPERCASE variable names. Reserve that for macro names. Cheers & hth., and do get yourself a good C++ beginners' book!, – Cheers and hth. - Alf Feb 13 '12 at 00:27

2 Answers2

6

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 3
    -1 "you can't call a constructor on an already-created object." is incorrect, and the answer does not constructively help the OP fix things. However much I agree with the other sentiments expressed here, I have to downvote. Sorry. – Cheers and hth. - Alf Feb 13 '12 at 00:30
  • 6
    Why not move this answer to the **comment** section -- it is after all just commentary? – Cheers and hth. - Alf Feb 13 '12 at 00:39
  • @Alf: My answer is correct. You can create a new object reusing the same memory, but that is a new object. And "change the constructor into a normal member function" is a solution to the problem. – Ben Voigt Feb 13 '12 at 00:53
  • 1
    I've already told you that your, uh, comment, is *incorrect*. Having someone like me telling you that should be enough to get you going searching for how to do what you say cannot be done. As it happens you would not have had to search longer than simply reading this page in full. Cheers & hth., – Cheers and hth. - Alf Feb 13 '12 at 01:16
  • @Alf: I know fully well about placement `new`. It does NOT call a constructor on an object which is already fully constructed. It creates a new object in the same memory region. The old object's lifetime ends when the storage is reused. I can quote you chapter and verse from the Standard if you like. – Ben Voigt Feb 13 '12 at 02:54
  • I suspect you didn't know about placement new until you read the answer by "smparkes", where you in a comment advocate a much more reasonable solution than in this comment-answer. Anyway, shouting that it doesn't call a constructor is just dumb. It does. Perhaps you mean that the fine line between old and new formal object in the same memory region is something the OP should understand from your statement of impossible. Well that's even dumber, sorry. – Cheers and hth. - Alf Feb 13 '12 at 03:17
  • @AlfP.Steinbach: Since you know so much more about placement new than I do, perhaps you'd like to answer [my 14 month old question](http://stackoverflow.com/questions/4418220/legality-of-using-operator-delete-on-a-pointer-obtained-from-placement-new)? If not, please stop trolling. – Ben Voigt Feb 13 '12 at 03:21
  • 1
    I'm not sure what *exactly* you're trying to ask. But you're allocating with `malloc`. Then you must free with `free`. To obtain the general effect of a `delete` expression you must then first explicitly call the destructor on the object, like `p->T::~T()` (you have to look it up, I can't remember whether the qualification is required or forbidden, it's pretty arbitrary). Note that a placement `delete` operator is only called in one situation, namely where constructor throws from within a `new` expression. That's the in-practice for what I think you're asking. Cheers & hth, – Cheers and hth. - Alf Feb 13 '12 at 03:39
  • @Alf: You completely missed the point of my question. That's ok, James already confirmed that the Standard is worded wrong. I was just pointing out that your accusation that I wasn't aware of placement `new` was completely unfounded. – Ben Voigt Feb 13 '12 at 03:45
  • I share Johannes' bafflement on that. If you're not talking about the in-practice (covered by me here), and not about the formal (covered by Johannes' answer), then I don't understand. But no matter, the point is, your answer here is incorrect under any normal interpretation (the one requiring the OP to be a language lawyer familiar with formal object-hood is not normal). Cheers, and going looking for James comment/answer whatever to see what it is, – Cheers and hth. - Alf Feb 13 '12 at 03:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7599/discussion-between-ben-voigt-and-alf-p-steinbach) – Ben Voigt Feb 13 '12 at 03:53
  • @Alf: Placement new isn't a suitable tool for anyone who doesn't want to learn the intricacies of object lifetime. – Ben Voigt Feb 13 '12 at 03:58
  • agreed. see my answer to the question (it's a "comment" by SO designations). – Cheers and hth. - Alf Feb 13 '12 at 04:12
3

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.

smparkes
  • 13,807
  • 4
  • 36
  • 61
  • That code is not reconstructing anything. It is flat out illegal in C++. Your code is just working around a problem that does not exist in the first place. – pmr Feb 12 '12 at 23:22
  • 4
    Please don't suggest placement `new`. I think the appropriate thing would be a local non-pointer variable scoped inside the loop. – Ben Voigt Feb 12 '12 at 23:23
  • 1
    Well, I wouldn't say I was _suggesting_ it, just that if VS is accepting `IP->`constructor, I'm guessing that's what it's doing. But I'd never defend the whole thing; it's very weird and I can't really justify why anyone would write anything like this. But, then, I have no background in VS and I do see VS snippets all the time that look strange (though not quite this strange.) – smparkes Feb 12 '12 at 23:34
  • 4
    the OP's syntax was never valid. :-) – Cheers and hth. - Alf Feb 13 '12 at 00:30