-5

In my header file I declare structure

    typedef struct _PreprocessedImage
{
  cv::Rect straight;
  cv::Rect diagonal;
  bool empty = true;
...
...
} PreprocessedImage;

Then I create class with method

std::vector<float> processData(cv::Mat &image, bool drawRegions = false, PreprocessedImage &preproc);
.

Try to compile and got

"error: default argument missing for parameter 3"

But when I try to declare method with default value, like that:

 std::vector<float> processData(cv::Mat &image, bool drawRegions = false, PreprocessedImage &preproc = PreprocessedImage());
.

I got

"error: invalid initialization of non-const reference of type 'PreprocessedImage& {aka _PreprocessedImage&}' from an rvalue of type 'PreprocessedImage {aka _PreprocessedImage}'"

How can i fix it?

Martin York
  • 246,832
  • 83
  • 321
  • 542

2 Answers2

1

All parameters with defaults should be at the end of the list, so you need something like:

std::vector<float> processData(cv::Mat &image, PreprocessedImage &preproc, bool drawRegions = false);
.
Martin York
  • 246,832
  • 83
  • 321
  • 542
paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
0

To add to paxdiablo's answer.

Yes, the default-argument parameters must come last. Your attempt to work around this by also giving preproc a default argument failed because a temporary cannot bind to an lvalue reference (it would have to be const); besides, giving something a default "for the sake of it" is probably not what you wanted to do.

An alternative, that doesn't require re-arranging your existing function, is to write a forwarding overload instead of using default arguments:

std::vector<float> processData(cv::Mat& image, bool drawRegions, PreprocessedImage& preproc)
{
   /* ... */
}

std::vector<float> processData(cv::Mat& image, PreprocessedImage& preproc)
{
   return processData(image, false, preproc);
}

By the way, you don't need (or want) that antique C-style typedef struct A { ... } B syntax in C++ (unless you require direct C compatibility); you just want struct B. And, if you really must go for the former, you should pick a name that isn't reserved to the implementation.

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021