Solved

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

770 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