Solved

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

Posted on 2003-11-12
6
607 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:karthikd22
6 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 9732298
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 );
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9732342
...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 :-(
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 9732547
VC6 isn't capable to deduce array size and use it as template argument.

This code works on VC6 (thanks to rstaveley)

#include <iostream>
using namespace std;

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


template<typename T, size_t nSize>
T Minimum( T (*refArray))
{
    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 );
    int am = Minimum<int, sizeof(a)/sizeof(int)>( a );

    cout << am << endl;

    return 0;
}

But it isn't as comfortable as on VC7.

Regards, Alex
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 125 total points
ID: 9736480
Here's another work around method:

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


template<typename TA, typename T>
TA minimum( const T &refArray)
{
      const int nSize = sizeof(refArray)/sizeof(refArray[0]);
      TA 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(int, char**)
{
      int i = minimum<int>( 10, -9 );
      cout << i << endl;
      
      double d = minimum<double>( 9.4, 9.3 );
      cout << d << endl;
      
      int am = minimum<int>( a );
      cout << am << endl;
      
      return 0;
}
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249053
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question