0

I have the following code

int main() {
  int x[2][3] = {{1,2,3},{4,5,6}};
  struct testCase {
    int** x;
  };
  vector<testCase> v = {{x}};
}

testCase struct takes a 2D array as its member and I try to declare a vector of such struct. I compile the code with clang++ -std=c++11 -stdlib=libc++ test.cc and obtain the error

test.cc:31:20: error: no matching constructor for initialization of 'vector<testCase>'
  vector<testCase> v = {{x}};
               ^   ~~~~~

Since 2D array x essentially has type int**, I'm not sure why I hit such error in this use case.

Thanks in advance!

xxks-kkk
  • 2,336
  • 3
  • 28
  • 48
  • 1
    That's wrong: 2d array does not decay into int**. – Eugene Oct 15 '19 at 03:21
  • Thanks for the comment. So, to fix the issue, I need to use dynamic array with `new`? – xxks-kkk Oct 15 '19 at 03:27
  • @xxks-kkk Or use some other type than `int **` -- why do you think you need this specific type anyway? This seems like an [XY problem](https://meta.stackexchange.com/a/233676/218910). What is your _actual_ goal here? – cdhowie Oct 15 '19 at 03:28
  • Thanks. I'm writing some test cases for a function API with one of the input argument as `int**`; I don't have power to modify such function API. – xxks-kkk Oct 15 '19 at 03:32
  • @xxks-kkk In that case, the answer to the linked question provides a possible solution. Dynamic allocation is not necessary; pointers can point to stack-allocated values as well. – cdhowie Oct 15 '19 at 03:35

0 Answers0