2014-07-08 102 views
2

我想比较两个矩阵element-wise并返回一个矩阵,如果该位置中的元素匹配,则返回一个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

我在这里做错了什么?

回答

1

将此标记为重复的人是正确的。由于我没有完全理解C++ template关键字,所以出现了这个问题。

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; 
} 

Where and why do I have to put the "template" and "typename" keywords?

具有以下固定的问题更换功能

相关问题