-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
  • 257,169
  • 86
  • 333
  • 562

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
  • 257,169
  • 86
  • 333
  • 562
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
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
  • 378,754
  • 76
  • 643
  • 1,055