Solved

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

Posted on 2003-11-12
6
604 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now