简体   繁体   English

正向声明的类型和“已经声明为类类型的非类类型”

[英]Forward-declared type and “non-class type as already been declared as a class type”

I have problem with following code: 我有以下代码的问题:

  template <typename T>
  void foo(struct bar & b);
  struct bar {};
  int main(){}

It compiles successfuly on GCC, but fails on MSVC (2008) with following error: 它在GCC上成功编译,但在MSVC(2008)上失败并出现以下错误:

C2990: 'bar' : non-class type as already been declared as a class type

Is the code wrong or it's a bug in MSVC? 代码是错误的还是MSVC中的错误?

It works if I add struct bar; 如果我添加struct bar;它可以工作struct bar; before template definition. 在模板定义之前。

In most situations, a C (or C++ compiler) works strictly top-to-bottom on your source code. 在大多数情况下,C(或C ++编译器)在源代码上严格按照从上到下的方式工作。 So you need a forward declaration before you ever attempt to reference struct bar , otherwise the compiler will not know that it exists. 因此,在尝试引用struct bar之前需要一个前向声明 ,否则编译器将不知道它存在。

And we have our winner: 我们有赢家:

https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-and-non-class-type-as-already-been-declared-as-a-class-type https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-and-non-class-type-as-already-been-declared-as-a-class-type

Thank you for reporting this issue. 感谢您报告此问题。 This is indeed a case of non-conformant behaviour in VC++. 这确实是VC ++中不符合行为的情况。 However, a simple workaround is to reorder the declarations so that the declaration "struct bar" is known when the template declaration is encountered. 但是,一个简单的解决方法是重新排序声明,以便在遇到模板声明时知道声明“struct bar”。 Due to the low severity of this bug and our priorities, we regret that we cannot fix the bug in the next release of the compiler but we will consider it for a future release. 由于此错误的严重性较低以及我们的优先级,我们很遗憾我们无法在下一版本的编译器中修复该错误,但我们会考虑将其用于将来的版本。

Regards, 问候,

Tanveer Gani Visual C++ Team Tanveer Gani Visual C ++团队

You most likely have struct bar {}; 你很可能有struct bar {}; somewhere above this block of code (possibly in a header file). 在这个代码块之上的某个地方(可能在头文件中)。 See http://msdn.microsoft.com/en-us/library/zfcw8kk9.aspx 请参阅http://msdn.microsoft.com/en-us/library/zfcw8kk9.aspx

Edit: Also from the link above: 编辑:同样来自上面的链接:

C2990 can also occur due to a breaking change in the Visual C++ compiler for Visual C++ 2005; C2990也可能由于Visual C ++ 2005的Visual C ++编译器发生重大变化而发生; the compiler now requires that multiple declarations for the same type be identical with respect to template specification. 编译器现在要求相同类型的多个声明在模板规范方面是相同的。

Since foo is templated and bar is being "forward-declared" in the foo argument list, what happens if you move struct bar {}; 由于foo是模板化的,并且barfoo参数列表中被“前向声明”,因此如果移动struct bar {};会发生什么struct bar {}; above foo ? 以上foo

That looks like valid code. 这看起来像有效的代码。 Whatever MSVC is doing, it appears to be some weird non-conforming behavior, from what I can see. 无论MSVC做什么,从我所看到的,它似乎是一些奇怪的不合规行为。

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

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