0

I've been doing a problem that involves rotating square arrays in c++. The length of the array is in 'len'. However, these rotate and flip methods return a 2d array and need 2d array arguments. The compiler gives me this error:

error: declaration of 'a' as multidimensional array must have bounds for all dimensions except the first.

However, the size of the array depends on the input - and when I have the input 'len' and declare the method, it gives me the old 'variable not declared in this scope' problem.

Pretty much, I can't say the length of the array because it changes depending on the input.

Is there any way to get past this?

#include <iostream>          
#include <fstream>
#include <cmath>

using namespace std;

class transform{
private:
    int len;
public:
    bool rot90[][](bool a[][]);
    bool rot180[][] (bool a[][]);
    bool check (bool a[][], bool b[][]);
    bool rot270[][] (bool a[][]);
    bool flip[][] (bool a[][]);
};

bool transform::check(bool a[len][len], bool b[len][len]){
    for(int h=0; h<len; h++){
        for(int w=0; w<len; w++){
            if(a[h][w] != b[h][w])
                return false;
        }
    }
    return true;
}


bool transform::rot90[len][len] (bool a[len][len]){
    bool[len][len] b;
    for(int h=0; h<len; h++){
        for(int w<0; w<len; w++){
            b[w][len-1-h] = a[h][w];
        }
    }
    return b;
}

bool transform::rot270[len][len] (bool a[len][len]){
    bool[len][len] b;
    for(int h=0; h<len; h++){
        for(int w<0; w<len; w++){
            b[len-1-w][h] = a[h][w];
        }
    }
    return b;
}

bool transform::flip[len][len] (bool a[len][len]){
    bool[len][len] b;
    for(int h=0; h<len; h++){
        for(int w<0; w<len; w++){
            b[h][len-1-w] = a[h][w];
        }
    }
    return b;
}

... code continues

NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
Mason Wang
  • 165
  • 1
  • 9
  • 2
    No. C++ does not work this way. Use a two-dimensional `std::vector` instead. – Sam Varshavchik Jun 02 '16 at 00:39
  • `vector>`(Warning! lack of contiguous data may result in poor cache usage) or make yourself a wrapper class around around your 2D array. Final caveat: resizable arrays are not supported by the C++ standard. Your compiler might allow it, but your portability options will be reduced. I tend to use something like this to get the best of both worlds: https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op – user4581301 Jun 02 '16 at 00:52
  • Have you considered [dynamically allocating arrays](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new)? – NonCreature0714 Jun 02 '16 at 02:37
  • @SamVarshavchik has the best suggestion, to use vectors, for you. – NonCreature0714 Jun 02 '16 at 04:46
  • Instead of returning an array why not do it the old fashioned way and just pass in the array sizes and an array of the correct size for the result. Then you don't have to allocate any dynamic array and it is up to the caller to allocate something of the correct size. – cup Jun 02 '16 at 04:48

0 Answers0