Error when trying to make 4d array in C++ (using std::vector and Eigen matrices)

huangapple go评论60阅读模式
英文:

Error when trying to make 4d array in C++ (using std::vector and Eigen matrices)

问题

以下是您要的代码部分的中文翻译:

我正在尝试创建一个4D数组,如下所示:

首先,我创建一个用于创建3D数组的函数,初始化为所有元素都为零(这部分正常工作):

std::vector<Eigen::Matrix<double, -1, -1>> vec_of_mats_test(int n_rows, int n_cols, int n_mats) {

  std::vector<Eigen::Matrix<double, -1, -1>> my_vec(n_mats);
  Eigen::Matrix<double, -1, -1> mat_sizes(n_rows, n_cols);

  for (int c = 0; c < n_mats; c++) {
    my_vec[c] = mat_sizes;
    for (int i = 0; i < n_rows; i++) {
      for (int j = 0; j < n_cols; j++) {
         my_vec[c](i,j) = 0;
      }
    }
  }

  return my_vec;

}

然后,我有以下函数,调用上面的函数以创建一个4D数组

std::vector<std::vector<Eigen::Matrix<double, -1, -1>>> 4d_array(int n_rows, int n_cols, int n_mats, int n_vecs) {

   std::vector<std::vector<Eigen::Matrix<double, -1, -1>>> my_4d_array(n_vecs);

   for (int c = 0; c < n_vecs; c++) {

     my_4d_array[c] = vec_of_mats_test(n_rows, n_cols, n_mats);

   }

   return my_4d_array;

}

然而,在编译过程中,我遇到了以下错误:

"在数字常量之前需要未限定的标识符"

我做错了什么?

谢谢
英文:

I am trying to make a 4d array as follows:

First I make a function to create a 3d array, initialised so all the elements are zero (which works fine):

std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; vec_of_mats_test(int n_rows,
                                                              int n_cols,
                                                              int n_mats) {


  std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; my_vec(n_mats);
  Eigen::Matrix&lt;double, -1, -1 &gt; mat_sizes(n_rows, n_cols);

  
  for (int c = 0; c &lt; n_mats; c++) {
    my_vec[c] = mat_sizes;
    for (int i = 0; i &lt; n_rows; i++) {
      for (int j = 0; j &lt; n_cols; j++) {
         my_vec[c](i,j) = 0;
      }
    }
  }


  return(my_vec);

}

Then, I have the following function which calls the function above to make a 4d array:

 std::vector&lt;std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; &gt;  4d_array(         int n_rows,
                                                                               int n_cols,
                                                                               int n_mats,
                                                                               int n_vecs) {
   

   std::vector&lt;std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; &gt; my_4d_array(n_vecs);
   
   for (int c = 0; c &lt; n_vecs; c++) {

     my_4d_array[c] = vec_of_mats_test(n_rows, n_cols, n_mats);
     
   }
   
   
   return(my_4d_array);
   
}

However , during compilation I get the following error:

"expected unqualified-id before numeric constant"

What am I doing wrong?

Thanks

答案1

得分: 3

4d_array不是C++中的有效标识符名称,因为你不能在标识符的第一个字符使用数字。

请使用另一个有效的名称。

英文:

4d_array is not a valid identifier name in C++ because you cannot use a number as the first character of an identifier.

Use another valid name instead.

huangapple
  • 本文由 发表于 2023年3月7日 21:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定