简体   繁体   English

使用模板编译MS VC ++代码时出现GCC错误

[英]GCC error when compiling MS VC++ code with templates

we're taking some code written for Visual Studio 2008 and try to compile it with gcc. 我们正在为Visual Studio 2008编写一些代码,并尝试使用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 alone T::NodeVector it doesn't know that magnitude is supposed to be a template. You need to specify that explicitly: 问题在于,由于C ++编译器不知道T的实际类型(更不用说T::NodeVector所以它不知道magnitude应该是模板。您需要明确指定:

x.template magnitude<double>();

Otherwise C++ will parse the tokens as x , operator. 否则,C ++会将标记解析为x operator. , magnitude , operator< , double , operator> magnitudeoperator<doubleoperator>

The GCC is right, by the way. 顺便说一句,海湾合作委员会是正确的。 MSVC++ is notoriously lax on such matters. 众所周知,MSVC ++在此类问题上松懈。

在B点,它无法知道x是什么类型,并且该大小将是模板函数,因此您需要将其声明为first。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM