none
error compiling C2061: identifier _CountofType

    Question

  •  

    I have a complex application that we are porting to Visual Studio 8 and have run into this error with no solution so far.

    Note that the error producing code resides in the file: 

    "D:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdlib.h

    This simple unit test demonstrates the problem as succinctly as possible I think

    // foo.cpp : Defines the entry point for the console application.

    //

     

    #include "stdafx.h"

     

    template < _CountofType, size_t _SizeOfArray>

    char (*__countof_helper( _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];

     

    int _tmain(int argc, _TCHAR* argv[])

    {

          printf( "hello world\n" );

          return 0;

    }

     

    1>------ Build started: Project: foo, Configuration: Debug Win32 ------

    1>Compiling...

    1>foo.cpp

    1>c:\documents and settings\jlarson\my documents\visual studio 2005\projects\foo\foo\foo.cpp(6) : error C2061: syntax error : identifier '_CountofType'

    1>c:\documents and settings\jlarson\my documents\visual studio 2005\projects\foo\foo\foo.cpp(7) : fatal error C1903: unable to recover from previous error(s); stopping compilation

    1>Build log was saved at "file://c:\Documents and Settings\jlarson\My Documents\Visual Studio 2005\Projects\foo\foo\Debug\BuildLog.htm"

    1>foo - 2 error(s), 0 warning(s)

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

     

     

    Friday, January 05, 2007 11:06 PM

Answers

  • OK, this is odd. I have those lines in my header files and I never seen these errors.

    So lets get back to you first example, if you add typename does it compile ?

    The only thing that comes to my mind now is that is something wrong with the C++ compiler, something like using an older version. Do you have another version of Visual Studio installed ? Or another C++ compiler ?

     

    Saturday, January 06, 2007 12:45 AM
    Moderator
  • You can try to put the following lines in a Visual C++ project and compile to see what happens:

    #if _MSC_VER < 1400

    #error You are using an old compiler.

    #endif

    You'll get an error if you are using a compuler other than the Visual C++ 2005 compiler (which version is > 14.0).

    But since you say that the code from your first post compiles with typename added I doubt it's the compiler. Maybe it's a header conflict with Visual C++ 6.0. There was an option when installing Visual C++ 6.0 to set environment variables like PATH, INCLUDE and LIB to point to VC++ 6.0 directories. If that is in effect maybe VC++ 2005 includes some old header from VC++ 6.0 (altough from the error messages you have posted it seems that it is using the correct include dir but maybe it's getting just one header from the old dir).

     

     

    Saturday, January 06, 2007 10:55 AM
    Moderator
  • Thank you,  I had to plow ahead and haven't tried your suggestion as the workaround worked well enough for me.

     

     my workaround was to do this:

    #ifndef _countof
    #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
    #endif

    #ifndef SORTPP_PASS
    #define SORTPP_PASS
    #endif

     

     

    Friday, January 26, 2007 12:08 AM

All replies

  • "Note that the error producing code resides in the file: 

    "D:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdlib.h"

    I see only 2 errors and both of them are in foo.cpp.

    As for the error itself:

    What is _CountofType supposed to be anyway ? A type ? Then you need to use typename in front of it:

    template <typename _CountofType, size_t _SizeOfArray>

     

     

    Friday, January 05, 2007 11:27 PM
    Moderator
  • You're right.  I don't know how I made that typo.  I don't know quite how to get a unit test to demonstrate the problem yet.

    D:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdlib.h(395) : error C2061: syntax error : identifier '_CountofType'
    D:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdlib.h(396) : fatal error C1903: unable to recover from previous error(s);
     stopping compilation

    Here is that code from stdlib.h

    #if !defined(_countof)

    #if !defined(__cplusplus)

    #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))

    #else

    extern "C++"

    {

    template <typename _CountofType, size_t _SizeOfArray>

    char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];

    #define _countof(_Array) sizeof(*__countof_helper(_Array))

    }

    #endif

    #endif

     

    I'm not sure why it's coming up.  At any rate, I can get rid of the error by doing this:

    #ifndef _countof

    #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))

    #endif

     

    But when I do that, I get another error:

    D:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(893) : error C2061: syntax error : identifier 'T'

    D:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(894) : fatal error C1903: unable to recover from previou

     

    Here is that code:

     

    #if defined(__cplusplus) && \

        !defined(MIDL_PASS) && \

        !defined(RC_INVOKED) && \

        !defined(_PREFAST_) && \

        (_MSC_FULL_VER >= 13009466) && \

        !defined(SORTPP_PASS)

    // ...

    //

    extern "C++" // templates cannot be declared to have 'C' linkage

    template <typename T, size_t N>

    char (*RtlpNumberOf( UNALIGNED T (&)No ))No;

     

    #define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))

    then to get rid of that, I can add

    #ifndef SORTPP_PASS

    #define SORTPP_PASS

    #endif

     

    and make it go away 

    Maybe if I can figure out what defines _countof  and SORTPP_PASS I can make it go away.

     

    thanks for any thoughts

    -Julie

     

     

    Friday, January 05, 2007 11:55 PM
  • OK, this is odd. I have those lines in my header files and I never seen these errors.

    So lets get back to you first example, if you add typename does it compile ?

    The only thing that comes to my mind now is that is something wrong with the C++ compiler, something like using an older version. Do you have another version of Visual Studio installed ? Or another C++ compiler ?

     

    Saturday, January 06, 2007 12:45 AM
    Moderator
  •  Mike Danes wrote:

    OK, this is odd. I have those lines in my header files and I never seen these errors.

    So lets get back to you first example, if you add typename does it compile ?

    yes, it does compile with the change...

     Mike Danes wrote:

    The only thing that comes to my mind now is that is something wrong with the C++ compiler, something like using an older version. Do you have another version of Visual Studio installed ? Or another C++ compiler ?

     

    I do.  I have visual studio 6 installed.  You think maybe somehow it is getting called somehow?  I dunno.  I guess I'll go with my workaround for now

    Thanks much!

    -Julie

     

    Saturday, January 06, 2007 1:14 AM
  • You can try to put the following lines in a Visual C++ project and compile to see what happens:

    #if _MSC_VER < 1400

    #error You are using an old compiler.

    #endif

    You'll get an error if you are using a compuler other than the Visual C++ 2005 compiler (which version is > 14.0).

    But since you say that the code from your first post compiles with typename added I doubt it's the compiler. Maybe it's a header conflict with Visual C++ 6.0. There was an option when installing Visual C++ 6.0 to set environment variables like PATH, INCLUDE and LIB to point to VC++ 6.0 directories. If that is in effect maybe VC++ 2005 includes some old header from VC++ 6.0 (altough from the error messages you have posted it seems that it is using the correct include dir but maybe it's getting just one header from the old dir).

     

     

    Saturday, January 06, 2007 10:55 AM
    Moderator
  • Hello

     

    Re: error compiling C2061: identifier _CountofType

     

    I am going to mark this thread as answered since you have not followed up with any further information on your problem as requested - I assume one of the suggestions in this thread helped you solved the problem. If you have a solution you could post it so others can find it. If you do not have a solution then please submit further details and then mark the thread as unanswered.

     

    Thanks

    Damien

    Thursday, January 25, 2007 9:55 PM
    Moderator
  • Thank you,  I had to plow ahead and haven't tried your suggestion as the workaround worked well enough for me.

     

     my workaround was to do this:

    #ifndef _countof
    #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
    #endif

    #ifndef SORTPP_PASS
    #define SORTPP_PASS
    #endif

     

     

    Friday, January 26, 2007 12:08 AM
  • I have same problem.

    I have installed VS 2005 and VS 2010, I used VS 2010.

    Can you give me step by step to fix this error? What must I do with the configuration VS 2010? 

    Thank's before.

    Monday, October 22, 2012 8:16 AM
  • This thread is quite old and it was never clear what the actual problem was. It would be better to start a new thread and provide the following details:

    - the exact error message you get (including file and line number)

    - what version of VS gives the error? 2005 or 2010?

    - which one was installed first?

    Monday, October 22, 2012 12:09 PM
    Moderator