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

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

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

问题

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

  1. 我正在尝试创建一个4D数组,如下所示:
  2. 首先,我创建一个用于创建3D数组的函数,初始化为所有元素都为零(这部分正常工作):
  3. std::vector<Eigen::Matrix<double, -1, -1>> vec_of_mats_test(int n_rows, int n_cols, int n_mats) {
  4. std::vector<Eigen::Matrix<double, -1, -1>> my_vec(n_mats);
  5. Eigen::Matrix<double, -1, -1> mat_sizes(n_rows, n_cols);
  6. for (int c = 0; c < n_mats; c++) {
  7. my_vec[c] = mat_sizes;
  8. for (int i = 0; i < n_rows; i++) {
  9. for (int j = 0; j < n_cols; j++) {
  10. my_vec[c](i,j) = 0;
  11. }
  12. }
  13. }
  14. return my_vec;
  15. }
  16. 然后,我有以下函数,调用上面的函数以创建一个4D数组
  17. std::vector<std::vector<Eigen::Matrix<double, -1, -1>>> 4d_array(int n_rows, int n_cols, int n_mats, int n_vecs) {
  18. std::vector<std::vector<Eigen::Matrix<double, -1, -1>>> my_4d_array(n_vecs);
  19. for (int c = 0; c < n_vecs; c++) {
  20. my_4d_array[c] = vec_of_mats_test(n_rows, n_cols, n_mats);
  21. }
  22. return my_4d_array;
  23. }
  24. 然而,在编译过程中,我遇到了以下错误:
  25. "在数字常量之前需要未限定的标识符"
  26. 我做错了什么?
  27. 谢谢
英文:

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):

  1. std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; vec_of_mats_test(int n_rows,
  2. int n_cols,
  3. int n_mats) {
  4. std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; my_vec(n_mats);
  5. Eigen::Matrix&lt;double, -1, -1 &gt; mat_sizes(n_rows, n_cols);
  6. for (int c = 0; c &lt; n_mats; c++) {
  7. my_vec[c] = mat_sizes;
  8. for (int i = 0; i &lt; n_rows; i++) {
  9. for (int j = 0; j &lt; n_cols; j++) {
  10. my_vec[c](i,j) = 0;
  11. }
  12. }
  13. }
  14. return(my_vec);
  15. }

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

  1. std::vector&lt;std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; &gt; 4d_array( int n_rows,
  2. int n_cols,
  3. int n_mats,
  4. int n_vecs) {
  5. std::vector&lt;std::vector&lt;Eigen::Matrix&lt;double, -1, -1 &gt; &gt; &gt; my_4d_array(n_vecs);
  6. for (int c = 0; c &lt; n_vecs; c++) {
  7. my_4d_array[c] = vec_of_mats_test(n_rows, n_cols, n_mats);
  8. }
  9. return(my_4d_array);
  10. }

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:

确定