2

I am trying to write a function in c++ that will take 2 input std::arrays, and return an array of the products via matrix multiplication. However, the function cannot take an array with different dimensions (ex. 4x4 works, 3x4 does not)

here is the code:

#include <iostream> 
#include <array>


template <std::size_t SIZE>
void dot(std::array<std::array<double, SIZE>, SIZE>& array1, 
     std::array<std::array<double, SIZE>, SIZE>& array2)
{
  int x1 = array1.size();
  int y1 = array1[0].size();


  int x2 = array2.size();
  int y2 = array2[0].size();
}

int main()
{
  using std::array;
  array<array<double, 4>, 4> syn0 = {{ {1,2,4},
                       {2,3,4},
                       {6,8,6},
                       {1,2,4} }};
  dot(syn0, syn0);
  return 0;
}

Using the template example as posed in this question, it will accept arrays such as 4x4 like in the code.

Changing the matrix to unequal numbers yields the following errors:

newer.cpp: In function ‘int main()’:
newer.cpp:23:21: error: too many initializers for ‘std::__array_traits<std::array<double, 4ul>, 3ul>::_Type {aka std::array<double, 4ul> [3]}’
            {1,2,4} }};
                     ^
newer.cpp:24:17: error: no matching function for call to ‘dot(std::array<std::array<double, 4ul>, 3ul>&, std::array<std::array<double, 4ul>, 3ul>&)’
   dot(syn0, syn0);
                 ^
newer.cpp:24:17: note: candidate is:
newer.cpp:6:6: note: template<long unsigned int SIZE> void dot(std::array<std::array<double, SIZE>, SIZE>&, std::array<std::array<double, SIZE>, SIZE>&)
 void dot(std::array<std::array<double, SIZE>, SIZE>& array1, 
      ^
newer.cpp:6:6: note:   template argument deduction/substitution failed:
newer.cpp:24:17: note:   deduced conflicting values for non-type parameter ‘SIZE’ (‘4ul’ and ‘3ul’)
   dot(syn0, syn0);
                 ^
newer.cpp:24:17: note:   ‘std::array<std::array<double, 4ul>, 3ul>’ is not derived from ‘std::array<std::array<double, SIZE>, SIZE>

I assume the reason for this is that the template only assigns one variable, so if i assign 2 to the same one, it throws the error. I tested to see if i could stack templates for two different variables, but that is not possible.

How can I allow the function to take a 2d array of any size and not cause that error?

1
  • oops sorry ill update the code. That was me testing if i could pass a second template variable. Commented Aug 16, 2016 at 21:34

1 Answer 1

4

Seems that you were really close.

You just need to add SIZE2 as a parameter to your template:

template <std::size_t SIZE,std::size_t SIZE2>
void dot(std::array<std::array<double, SIZE>, SIZE2>& array1, 
     std::array<std::array<double, SIZE>, SIZE2>& array2)

and it compiles all right BTW your syn0 size was wrong

int main()
{
  using std::array;
  array<array<double, 3>, 4> syn0 = {{ {1,2,4},
                       {2,3,4},
                       {6,8,6},
                       {1,2,4} }};
  dot(syn0, syn0);
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.