C++ 为什么下面的Eigen示例获胜';不编译?

C++ 为什么下面的Eigen示例获胜';不编译?,c++,eigen,C++,Eigen,我正在尝试编译此MWE,但出现了很多错误: #include <eigen/Eigen/Core> #include <eigen/unsupported/Eigen/CXX11/Tensor> #include <array> using namespace Eigen; int main() { // Create 2 matrices using tensors of rank 2 Eigen::Tensor<int, 2> a(2, 3

我正在尝试编译此MWE,但出现了很多错误:

#include <eigen/Eigen/Core>
#include <eigen/unsupported/Eigen/CXX11/Tensor>
#include <array>

using namespace Eigen;

int main()
{
// Create 2 matrices using tensors of rank 2
Eigen::Tensor<int, 2> a(2, 3);
a.setValues({{1, 2, 3}, {6, 5, 4}});
Eigen::Tensor<int, 2> b(3, 2);
a.setValues({{1, 2}, {4, 5}, {5, 6}});

// Compute the traditional matrix product
array<IndexPair<int>, 1> product_dims = { IndexPair<int>(1, 0) };
Eigen::Tensor<int, 2> AB = a.contract(b, product_dims);

// Compute the product of the transpose of the matrices
array<IndexPair<int>, 1> transpose_product_dims = { IndexPair<int>(0, 1) };
Eigen::Tensor<int, 2> AtBt = a.contract(b, transpose_product_dims);
}
#包括
#包括
#包括

关于收缩,但我认为它有一些错误,并且没有正确编译,这是我试图修复的

错误:

1.cc:11:3: error: no member named 'setValues' in 'Eigen::Tensor<int, 2, 0, long>'
a.setValues({{1, 2, 3}, {6, 5, 4}});
~ ^
1.cc:11:13: error: expected expression
a.setValues({{1, 2, 3}, {6, 5, 4}});
            ^
1.cc:13:3: error: no member named 'setValues' in 'Eigen::Tensor<int, 2, 0, long>'
a.setValues({{1, 2}, {4, 5}, {5, 6}});
~ ^
1.cc:13:13: error: expected expression
a.setValues({{1, 2}, {4, 5}, {5, 6}});
            ^
1.cc:16:26: error: non-aggregate type 'array<IndexPair<int>, 1>' cannot be initialized with an initializer list
array<IndexPair<int>, 1> product_dims = { IndexPair<int>(1, 0) };
                         ^              ~~~~~~~~~~~~~~~~~~~~~~~~
1.cc:20:26: error: non-aggregate type 'array<IndexPair<int>, 1>' cannot be initialized with an initializer list
array<IndexPair<int>, 1> transpose_product_dims = { IndexPair<int>(0, 1) };
                         ^                        ~~~~~~~~~~~~~~~~~~~~~~~~
6 errors generated.
1.cc:11:3:错误:“Eigen::Tensor”中没有名为“setValues”的成员
a、 集合值({1,2,3},{6,5,4});
~ ^
1.cc:11:13:错误:应为表达式
a、 集合值({1,2,3},{6,5,4});
^
1.cc:13:3:错误:“Eigen::Tensor”中没有名为“setValues”的成员
a、 集合值({1,2},{4,5},{5,6});
~ ^
1.cc:13:13:错误:应为表达式
a、 集合值({1,2},{4,5},{5,6});
^
1.cc:16:26:错误:无法使用初始值设定项列表初始化非聚合类型“array”
数组乘积_dims={IndexPair(1,0)};
^              ~~~~~~~~~~~~~~~~~~~~~~~~
1.cc:20:26:错误:无法使用初始值设定项列表初始化非聚合类型“array”
数组转置乘积dims={IndexPair(0,1)};
^                        ~~~~~~~~~~~~~~~~~~~~~~~~
生成6个错误。

此示例需要c++11,因此您需要在编译器上启用它,例如在gcc 6或clang之前使用带有gcc的
-std=c++11

。你在定义那个宏吗?不,我甚至不知道它是什么意思?我得查一下。但我更担心的是收缩尝试不起作用,而不是设置值,我无论如何都不打算使用它。在包含之前添加
\define EIGEN\u有可变的模板1
(或者,将
-DEIGEN\u有可变的模板=1
传递到gcc/clang)。