0

Im currently working on Opencv 3.0 C++ in Xcode 7.2. I have and code error written

Variable length array of non-POD element type cv::Mat

The sample code was define as below

Mat symbol[opts.numofblocksX*opts.numofblocksY];

I have change the code to

Mat symbol = symbol[opts.numofblocksX * opts.numofblocksY];

and it show another error

cv::Mat doest not provide a subscript operator

Is there anyone facing the same problem before? what was the solutions I can implement here?

Thanks

D_9268
  • 1,039
  • 2
  • 9
  • 17
  • What are you trying to do? If you want to create an array of `Mat`, you can do: `std::vector symbols(how_many);` – Miki Mar 06 '16 at 13:56

1 Answers1

0

This code:

cv::Mat symbol[opts.numofblocksX*opts.numofblocksY];

Defines an array of Mats of size opts.numofblocksX*opts.numofblocksY.

The error you got is because the size of this array is not fixed at compile time and this is not a POD type.

Your new code is flawed.

cv::Mat symbol = symbol[opts.numofblocksX * opts.numofblocksY];

This defines a single Mat called symbol, and then nonsensically tries to call operator[] on it with opts.numofblocksX * opts.numofblocksY as an argument. This is not declaring an array.

Two obvious options present themselves:

  • Allocate your array on the heap, where variable-size allocation is allowed. Don't forget to delete[] it later! (or use a smart pointer)

    cv::Mat *symbol = new cv::Mat[opts.numofblocksX * opts.numofblocksY];

  • Use an std::vector (recommended):

    std::vector<cv::Mat> symbols(opts.numofblocksX * opts.numofblocksY]);

Community
  • 1
  • 1
Chris Kitching
  • 2,559
  • 23
  • 37