0

I am having problems passing a multidimensonal array to a function from main. Here is an example of the problem:

double function(int**);
int main(){
    vector< vector<int> > my_vec;
    double result;
    result = funtion(my_vec); //it doesnt recognize the type. my vec
    return 0;
}
double function(int**my_vec){
    // DOES STUFF WITH THE DATA
}

What is the correct way of passing the matrix to the function?

quamrana
  • 27,260
  • 11
  • 50
  • 62
Duccio Piovani
  • 880
  • 2
  • 11
  • 24
  • Redeclaring the function to take the correct type by-reference springs to mind. those two types (`int**` and `std::vector>` are quite-literally *worlds* apart. – WhozCraig Feb 03 '15 at 20:04
  • You are definitely right !! now that I know the answer I dont know why I was trying int** !! – Duccio Piovani Feb 03 '15 at 20:59
  • Please [accept](http://meta.stackexchange.com/questions/5234) an answer if you think it solves your problem. It will community at large to recognize the correct solution. This can be done by clicking the green check mark next to the answer. See this [image](http://i.stack.imgur.com/uqJeW.png) for reference. Cheers. – Bhargav Rao Dec 22 '15 at 16:11

3 Answers3

4

What is the correct way of passing the matrix to the function ??

Change the signature of function to:

double function(vector< vector<int> >& my_vec);
R Sahu
  • 196,807
  • 13
  • 136
  • 247
2

The correct way to accept the argument is:

double function(vector<vector<int>> const &);

unless the function needs to modify the argument, in which case use:

double function(vector<vector<int>> &);

The int** type signature is for raw C-style arrays: there's no reason to discard the helpful C++ container here.

Useless
  • 55,472
  • 5
  • 73
  • 117
0

Std::vector is not just an array. It is STL type, whitch simulates dynamic array. What you are passing is simple 2D array like int arr[3][3]. To pass vector you need to change your function header to double function(vector< vector<int>> &vec)(or maybe double function(vector< vector<int>> vec) - depends on what you want to do)

newfolder
  • 108
  • 2
  • 8