Search code examples
c++opencv3.0vlfeat

opencv 3.0 Mat not provide a subscript operator


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


Solution

  • 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]);