0

May I have a question as "Why following code compile fail with error C2371 at line 04" ?

It succeeded on VS6 but failed on VS2008.

I've tried to comment out line 05, still have this compile error.

01   typedef struct tagAAA
02   {
03       unsigned char      a;
04   } AAA;

05   typedef AAA FAR* LPAAA;

Thanks in advance.

3
  • Because of something that came before. Nov 16, 2012 at 12:45
  • 1
    Probably because AAA is defined somewhere else. As a test try changing AAA to something else and see of the error goes away. Also might help to quote the exact error message.
    – john
    Nov 16, 2012 at 12:46
  • 3
    If you are using C++ to compile this code, you don't need the typedef struct as all structures, as classes, can be used as types. Nov 16, 2012 at 12:47

4 Answers 4

4

Presumably, the compiler gave an error message as well as a code, which told you what the problem is. Googling the code reveals:

'identifier' : redefinition; different basic types

You say that the error is on line 4; in that case, presumably the identifier is AAA, and you've already declared something else called AAA.

As to why it succeeds on a 15-year-old pre-standard compiler, you'll have to ask a historian.

Also, get rid of that typedef struct nonsense; it's a bad idea in C, and completely pointless in C++. And unless you need to support 16-bit MS-DOS, get rid of that FAR* nonsense too; you just need

struct AAA {
    unsigned char a;
};
1

Sorry guys, I was using the same name conflict with the one that already defined in Windows. It seems that VS6.0 was not including that file.

Thank you very much for all your support.

Another option is include following macro in the stdafx.h

define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

0

You don't show us the definition of FAR (which can only be a macro here). I think that there was such a macro in the Microsoft headers back in the 8086 (16 bit MS/DOS) days. I'm just guessing, but it wouldn't surprise me if they'd kept it around for a while to avoid breaking older code, and only got rid of it after VC6.

0

MSDN says that error is "The identifier is already declared."

Which means that the identifier, AAA, has already been declared.

So that's the reason.


In C++, simply write

struct Aaa
{
    unsigned char      a;
};

where

  • no tag (not needed in C++)

  • no typedef of pointer type (an abomination, and also causing problems with applying const correctly, e.g. in Microsoft’s own code),

  • no FAR (MS-DOS is a bit ancient).

Also, using a not-all-uppercase name, to minimize the possibility of conflict with a macro name.

Such a conflict might be involved in your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.