Link to home
Start Free TrialLog in
Avatar of karthikd22
karthikd22

asked on

C++ Template program - Compilation error with VC 6 Compiler??

Hello Friends,
         I got a sample program for C++ templates from the net and compiled it using Microsoft Visual C++ compiler ver 6.0 SP 5.
         I got the following errors:
D:\home\karthikd\temp\test.cpp(12) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
D:\home\karthikd\temp\test.cpp(36) : error C2784: 'T __cdecl minimum(T (&)[1])' : could not deduce template argument for ' (&)[1]' from 'int [6]'
D:\home\karthikd\temp\test.cpp(36) : error C2780: 'T __cdecl minimum(T,T)' : expects 2 arguments - 1 provided
        D:\home\karthikd\temp\test.cpp(5) : see declaration of 'minimum'

         The program I tried is below:

#include <iostream>
using namespace std;

template<class T>
T minimum( T a, T b )
{
    return a < b ? a : b;
}


template<typename T, int nSize>
T minimum( T (&refArray)[nSize] )
{
    T minVal = refArray[0];
    for( int i = 0; i < nSize; i++ )
    {
        if( refArray[i] < minVal )
        {
            minVal = refArray[i];
        }
    }

    return minVal;
}

int a[] = { 1, 2, 5, -9, 0, 22 };

int main( void )
{
    int i = minimum<int>( 10, -9 );
    cout << i << endl;

    double d = minimum<double>( 9.4, 9.3 );
    cout << d << endl;
   
    int am = minimum( a );
    cout << am << endl;

    return 0;
}

        But the same program compiles without any errors using Microsoft VC 7 compiler and g++(Red Hat
Linux) v3.0.4 compiler.
         Does somebody have any ideas what could be the problem with this code on VC 6 compiler?Are there any workarounds available for the above program to compile well with VC 6 compiler?
         I am having a  similar problem in one of my projects (porting some C++ code from Linux to Windows)
for which I am getting error C2780 as similar as above.There function overloading is involved.
        If needed I can post the snippet of the problematic project code.But I thought to try a sample program and solve the problem and then see whether I could apply the solution to my project code.
        Thanks for your help.I need to use VC 6 compiler only.

Thanks & Regards,
D.Karthik
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

It looks like the foillowing would be tricky for a compiler to figure out which template to use and how:

> int am = minimum( a );

This gives it a nudge in the right direction:

    int am = minimum<int,sizeof(a)/sizeof(int)>( a );
...sorry I can't test this on VC6, I've shot myself in the foot with a new firewall and can't VPN into the office :-(
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tinchos
tinchos

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: itsmeandnobodyelse {http:#9732547} & Axter {http:#9736480}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer