Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

Is the code wrong or it's a bug in MSVC?

It works if I add struct bar; before template definition.

like image 794
Hello C Plus Plus Avatar asked May 13 '11 17:05

Hello C Plus Plus


2 Answers

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

Thank you for reporting this issue. This is indeed a case of non-conformant behaviour in VC++. However, a simple workaround is to reorder the declarations so that the declaration "struct bar" is known when the template declaration is encountered. 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

like image 139
John Avatar answered Oct 31 '22 19:10

John


In most situations, a C (or C++ compiler) works strictly top-to-bottom on your source code. So you need a forward declaration before you ever attempt to reference struct bar, otherwise the compiler will not know that it exists.

like image 25
Oliver Charlesworth Avatar answered Oct 31 '22 19:10

Oliver Charlesworth