Compile time error for size parameter in templates

Hi,

The following code doesnt seem to compile

template <typename Type, int size>
Type min(Type (&arr)[size])
{

}

The error says
error C2265: '<Unknown>' : reference to a zero-sized array is illegal

Any suggestions

Thanks.
KarthikR



Answer this question

Compile time error for size parameter in templates

  • C. McGrunty

    The problemis that min might be already defined as a macro.

    try

    #undef min
    template <typename Type, int size>
    Type min(Type (&arr)[size])
    {
    }

    Or use an other name for min like mymin, xmin or whatever.



  • excittor

    rKarthik wrote:
    Martin,

    Thanks for the reply.
    Doesnt seem to help. I changed the name of the function to min1 and it still gives the same error.

    Thanks,
    KarthikR

    The following code compiles fine on VC 2005

    template <typename Type, int size> Type Min(Type (&arr)[size])
    {
    return ' ';
    }


    int main()
    {
    char y[10];
    Min<char, 10>(y);
    return 0;
    }



  • Bocman

    rKarthik wrote:
    Hi Nishant,

    I copied your exact code and it doesnt compile on Visual C++ 6.0,

    Anyways,

    Thanks all for the response.

    KarthikR

    Okay - VC 6 is pretty bad at handling some of the template related stuff. Upgrade to VC++ 2005 if you can, it's definitely worth it.



  • Vales

    Kevin,
    Sorry I should of replied to you. I replied to Nishant instead. But the point is that the same code doesnt compile on Visual C++ 6.0


  • Catherine Sea

    I believe the reason for your problem is stated in the error message:

    "reference to a zero-sized array is illegal".

    Make sure that you are passing in an array with well defined, non-zero size. See Nish's code abover for an example.



  • SuperNova298

    Martin,

    Thanks for the reply.
    Doesnt seem to help. I changed the name of the function to min1 and it still gives the same error.

    Thanks,
    KarthikR


  • vbman

    Forget VC6 and templates. It has horrible problems use the 2003 or 2005 compilers. The code you are asking for works on both.

  • Rafayel

    Hi Nishant,

    I copied your exact code and it doesnt compile on Visual C++ 6.0,

    Anyways,

    Thanks all for the response.

    KarthikR


  • Compile time error for size parameter in templates