Mysterious gcc compile error near Eigen `cast`

Alec Jacobson

December 14, 2013

weblog/

I run into this error often and it always takes me a bit to remember the problem. The line usually looks like:

MatrixXd B = A.cast<float>();

and the error gcc spits out is:

XXX.cpp:719:34: error: expected primary-expression before 'float'
XXX.cpp:719:34: error: expected ',' or ';' before 'float'

The problem happens when A is some templated Eigen object. The solution is to add the word template in front of cast:

MatrixXd B = A.template cast<float>();

Thanks for nothing gcc error messages.