0

I'm sure this is probably a duplicate question but I'm having difficulty passing a 2D array from one class into a function in a different class- it seems like its only expecting one argument rather than four. I'm kind of new to C++ so I might be missing something obvious. Here's basically what the code looks like:

//global variables
  int NN;


 class x
{
...

void w(const int arr[][cow], int pig, int cow, int NN) //my problem is in here, the compiler will come up with errors like there is an expected ) before the first [] in my array and that cow isn't a variable in the scope even though from what i can tell i shouldn't have to declare it if i'm passing it in
{
    ... //nothing in the actual block of code has been setting off errors so i don't think its anything in here 

}

};

class y
{
...

void e() //this function by itself compiles
{
    int arr[NN][NN + 1];
    int pig = 0;
    int cow = 0;
    x* temp = NULL;

    ...
        //the function call 
        temp->w(arr, pig, cow, NN);
    ...


}
};

If i need to post more code i can but i really think its just in how i passed it; thanks in advance for any help

caz
  • 1
  • 4
    `const int arr[][cow]` cannot be done. `cow` must be constant. Use a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) or [`std::array`](http://en.cppreference.com/w/cpp/container/array) instead. If these are outlawed, write a small wrapper class (eg `struct wrapper{ int ** array, int pigs; int cows; };`) to carry the array and its sizes. – user4581301 Apr 27 '16 at 22:43
  • Welcome to SO. Please break the long lines in your code into shorter ones – gdlmx Apr 27 '16 at 22:43
  • 2
    What precisely do you mean by "having difficulty"? What's the error you're getting? See [How to Ask](http://www.stackoverflow.com/help/how-to-ask). – CodeMouse92 Apr 27 '16 at 22:44
  • I also have to wonder: how much research did you do before posting? If you suspect it's a duplicate, you probably know that this has been answered before. [It has.](https://duckduckgo.com/?q=C%2B%2B+pass+2d+array&t=canonical&ia=qa) – CodeMouse92 Apr 27 '16 at 22:48
  • What you're trying to do is not only illegal in C++, it doesn't make any sense as something to want to do. Before you called ``temp->w`` you had already said that ``arr`` was an array of ``NN+1`` columns, but inside ``temp->w`` you want to say it is an array of ``cow`` columns? Why? – David K Apr 27 '16 at 22:50

0 Answers0