[ACCEPTED]-Warning C4099: type name first seen using 'class' now seen using 'struct' (MS VS 2k8)-compiler-warnings

Accepted answer
Score: 27

This warning appears when you have a one 20 type declaration that contradicts another 19 (one says "class", the other says "struct"). Given 18 the one definition rule, all declarations 17 except for at most one must be forward declarations. The 16 warning will generally indicate that a forward 15 declaration of a type is wrong and is usually 14 a simple typo and should be fixed. In this 13 case there should be no side effects, but 12 you really should fix it.

There can be, however, some 11 very nasty things happen if you have type 10 name clashes (perhaps caused by using "using 9 namespace" clauses or global namespace pollution). These 8 warnings could be indicating that you are 7 mixing headers from two different libraries 6 and the type names have clashes. Code compiled 5 under these conditions could do some very 4 unexpected things.

My advice - understand 3 why the warning has appeared and fix it. If 2 the warning is in a third party product, insist 1 that they fix it.

Score: 5

Just to bring the comment by MSalters against 6 this post above to the top level. I have had 5 several hard to find linker errors as a 4 result of VC using the 'class' or 'struct' keyword 3 in its mangling of names.

If you don't expect 2 it to be a problem you can be left scratching 1 your head for hours!

Score: 3

Richard Corden is correct - there is a reason 21 MS has this warning. For the MS compiler, decorated 20 (mangled) names include which class-key (struct or class) is 19 used to declare a type. If a function that 18 takes some object as an argument or returns 17 that object is referenced somewhere when 16 the wrong class-key is visible, you will 15 not get a compiler error but the linker 14 will complain because the decorated names 13 differ. The linker error only shows the 12 symbol it's looking for, and it's easy to 11 overlook the class-key mismatch there, so 10 the earlier, more detailed compiler warning 9 is valuable. It's still possible that the 8 two versions don't appear in the same compilation 7 unit, of course, and you will probably be 6 scratching your head for a while if you 5 believe that the only difference is default 4 member visibility.

The difference in mangling 3 conflicts with the C++ standard, which says 2 that forward declarations like struct Foo; and class Foo; are 1 equivalent, and so should use the same mangling.

Score: 3

I discuss this warning in depth in my blog 3 posting "Is C4099 really a sillywarning?". My conclusion is that it 2 is best turned off. :-) Well, at least for 1 me.

Score: 1

Although this is considered bad practice, I 7 think there should be no problem mixing 6 class definition and struct declaration, as 5 they are basically the same data type. The 4 main difference is that struct members are 3 by default public, contrary to class members 2 which are private, but otherwise the memory 1 layout is identical.

Score: 1

In c++ the only difference between a class and 10 a struct is that class's member variables, member 9 functions and base classes are private by 8 default, while in a struct they're by default 7 public; so, the fact that the class is POD 6 should not make any difference here.
I would 5 guess that this warning comes from code 4 maintenance (definition updated somewhere 3 but not somewhere else), and fix the code 2 so that the warning disappears (e.g. using 1 class in the typedef).

Score: 1

One thing I've seen that can cause this 5 warning is trying to #import the .tlb file 4 from a DLL while also having the same DLL 3 as a reference in your project. I just 2 fixed a problem with this by removing the 1 DLL as a reference from within my project.

Score: 0

It looks like there is a mismatch between 5 forward declaration and actual declaration. This 4 warning usually comes when you have declared 3 your forward declaration as struct but you declared 2 a class. Making your forward declaration also 1 a class would solve this warning.

More Related questions