Friday, 28 July 2017

C++ function calling to 2d array




Hey guys I have in class County.h constructor:



struct Country {

Country(double**, int);
};


and in main I have graph[Size][Size] and I want to call the constructor for County.



int main() {
double graph[Size][Size];
Country c(graph, 0);
}



But its giving me error no matching function for call to ‘County::County(double [22][22], int)’



What I can do in order to solve this problem? Thank you


Answer



double [Size][Size] and double** are not at all the same type. That is what your compiler doesn't like.



Change your constructor prototype or the way you declare your array. But you cannot directly cast an array of array to a pointer of pointer.




struct Country {
Country(double[Size][Size], int);
};


OR:



int main() {
double** graph = new (double*)[Size];
for (int i = 0; i < Size; ++i) {

graph[i] = new double[Size];
}
Country c(graph, 0);

// Don't forget to delete your graph then.
}


Note that the first one requires that you would know the size before your code start its execution (storing Size in a macro for instance), but the second one is longer to code, and you will have to manipulate more RAM memory, that can lead to mistakes if you are not careful.


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...