we’re taking some code written for Visual Studio 2008 and try to compile it with gcc. We experienced an error in the following code (simplified to what’s necessary):
template<int R, int C, typename T>
struct Vector
{
template <typename TRes>
TRes magnitude() const
{
return 0;
}
};
struct A
{
typedef Vector<3,1,int> NodeVector;
};
template<class T>
struct B
{
void foo()
{
typename T::NodeVector x;
x.magnitude<double>(); //< error here
}
};
...
B<A> test;
test.foo();
GCC says
error: expected primary-expression before 'double'
error: expected `;' before 'double'
Can you explain the error to me? What’s a cross-compiler solution?
Thanks a lot!
The problem is that since the C++ compiler doesn’t know the actual type of
T(let aloneT::NodeVectorit doesn’t know thatmagnitudeis supposed to be a template. You need to specify that explicitly:Otherwise C++ will parse the tokens as
x,operator.,magnitude,operator<,double,operator>…The GCC is right, by the way. MSVC++ is notoriously lax on such matters.