Solved

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

Posted on 2003-11-12
6
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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