Skyline_V
Skyline_V

Reputation: 35

Calling overloaded constructors fails in if statement

So I have an if statement that decides whether one or another constructor is called. This is the code that gives me the error:

detect::Wrapper detector;

if (detectorFileNameSpecified){
  detector = detect::Wrapper(detectorFileName,
             saveImages,
             learnOrDetect,
             verbosity);
}
else {
  detector = detect::Wrapper(saveImages,
             learnOrDetect,
             verbosity);
}

detector.setFind(findAlgNo);
detector.setLD(LDAlgNo);

detector.run();

g++ gives me these kinds of errors:

main.cpp:126:15: error: use of deleted function ‘detect::Wrapper& detect::Wrapper::operator=(detect::Wrapper&&)’
      verbosity);
               ^
In file included from main.cpp:1:0:
wrapper.hpp:14:9: note: ‘detect::Wrapper& detect::Wrapper::operator=(detect::Wrapper&&)’ is implicitly deleted because the default definition would be ill-formed:
   class Wrapper{
         ^~~~~~~

However if I do it the naive way of creating redundant code it works perfectly.

if (detectorFileNameSpecified){
      detect::Wrapper detector(detectorFileName,
                   saveImages,
                   learnOrDetect,
                   verbosity);

      detector.setFind(findAlgNo);
      detector.setLD(LDAlgNo);

      detector.run();
    }
    else {
      detect::Wrapper detector(saveImages,
                   learnOrDetect,
                   verbosity);

      detector.setFind(findAlgNo);
      detector.setLD(LDAlgNo);

      detector.run();
    }

All I want to do is reduce the redundancy in my code and not have to write the .setFind() etc. part every time I need a different constructor. I'm not sure how to solve this.

Upvotes: 0

Views: 70

Answers (2)

Jarod42
Jarod42

Reputation: 217275

By creating function:

void doJob(detect::Wrapper&& detector)
{
    detector.setFind(findAlgNo);
    detector.setLD(LDAlgNo);

    detector.run();
}

You may then factorize your code to:

if (detectorFileNameSpecified){
    doJob(detect::Wrapper(detectorFileName, saveImages, learnOrDetect, verbosity));
} else {
    doJob(detect::Wrapper(saveImages, learnOrDetect, verbosity));
}

Upvotes: 1

user0042
user0042

Reputation: 8018

What you can do to avoid code duplication is using a pointer and circumvent the deleted assignment operator:

std::unique_ptr<detect::Wrapper> detector; // <<<<<<<<<<<

if (detectorFileNameSpecified){
  detector = std::make_unique<detect::Wrapper>(detectorFileName,
                                               saveImages,
                                               learnOrDetect,
                                               verbosity);
}
else {
  detector = std::make_unique<detect::Wrapper>(saveImages,
                                               learnOrDetect,
                                               verbosity);
}

// Note the -> to dereference    
detector->setFind(findAlgNo);
detector->setLD(LDAlgNo);

detector->run();

Upvotes: 1

Related Questions