我想按元素比较两个矩阵并返回一个包含 1
的矩阵如果该位置的元素匹配,或者 0
否则。我创建了一个简单的测试函数来执行此操作:
template <class A, class B>
void func(const A& a, const B& b)
{
auto c = (b.array() == a.array()).cast<int>();
std::cout << c;
}
所以我写了一个main
像这样的功能来测试它:
int main()
{
Eigen::Array<int,Eigen::Dynamic,Eigen::Dynamic> b;
b.resize(2,2);
b.fill(2);
auto a = b;
a(0,0) = 1;
a(0,1) = 2;
a(1,0) = 3;
a(1,1) = 4;
func(a,b);
return 0;
}
但我一直收到这个错误:
eigenOperations.cpp: In function ‘void func(const A&, const B&)’:
eigenOperations.cpp:8:24: error: expected primary-expression before
‘int’ auto c = temp.cast<int>();
eigenOperations.cpp:8:24: error: expected ‘,’ or ‘;’ before ‘int’ make: *** [eigenOperations] Error 1
我在这里做错了什么?
最佳答案
将其标记为重复的人是对的。问题确实出现了,因为我没有完全理解 C++ template
关键字。
Where and why do I have to put the "template" and "typename" keywords?
将函数替换为以下修复了问题:
template <class A, class B>
void func(const A& a, const B& b)
{
auto c = (b.array() == a.array()).template cast<int>();
std::cout << c;
}
关于c++ - 如何比较两个动态数组并检查哪些元素匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624671/