tags:

views:

120

answers:

4

I have following code:

#include <iostream>
using namespace std;
template<class T>
T max(T *data,int len){
    int i;
    T Max=data[0];

     for (int i=1;i<len;i++){

         if (data[i]>max){
             Max=data[i];


     }
     }
 return  Max;

}
int mai(){
    int i[]={12,34,10,9,56,78,30};
    int len=sizeof(i)/sizeof(i[0]);
    cout<<max(i,len)<<"\n";



     return 0;
}

But when I compile it, I get the following errors:

generic_max, Configuration: Debug Win32 ------
1>Build started 7/29/2010 1:03:25 AM.
1>InitializeBuildStatus:
1>  Touching "Debug\generic_max.unsuccessfulbuild".
1>ClCompile:
1>  generic_max.cpp
1>c:\users\david\documents\visual studio 2010\projects\generic_max\generic_max\generic_max.cpp(10): error C2563: mismatch in formal parameter list
1>          c:\users\david\documents\visual studio 2010\projects\generic_max\generic_max\generic_max.cpp(22) : see reference to function template instantiation 'T max<int>(T *,int)' being compiled
1>          with
1>          [
1>              T=int
1>          ]
1>c:\users\david\documents\visual studio 2010\projects\generic_max\generic_max\generic_max.cpp(10): error C2568: '>' : unable to resolve function overload
1>          c:\users\david\documents\visual studio 2010\projects\generic_max\generic_max\generic_max.cpp(4): could be 'T max(T *,int)'
1>          c:\program files\microsoft visual studio 10.0\vc\include\xutility(2086): or       'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)'
1>          c:\program files\microsoft visual studio 10.0\vc\include\xutility(2078): or       'const _Ty &std::max(const _Ty &,const _Ty &)'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.95
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Please help me to fix this problem.

+4  A: 

C++ is case-sensitive.

#include <iostream>

// Note the omission of `using namespace std;`. The declaration can
// introduce clashes if one of your functions happen to have the same name
// as the functions in the std namespace.

template<class T> 
T max(T *data,int len) { 
    //int i; <-- Not needed? The for loop already has an `i` declared in it.
    T Max=data[0]; 

    for (int i=1;i<len;i++) { 
        /****** Note `Max`, not `max` ******/
        // The typo in your code snippet is what's causing the C2563.
        // `max` (in this context) refers to the function
        // `template<class T> T max(T *data,int len)` that has been declared.
        // `Max` refers to the variable declared in this function. 
        // (For the sake of readability, variable names should not similar
        // to the function name). 
        if (data[i]>Max) {
            Max=data[i]; 
        } 
    } 
    return  Max; 
} 

/****** Note `main()`, not `mai()` ******/
int main() {
    int i[]={12,34,10,9,56,78,30}; 
    int len=sizeof(i)/sizeof(i[0]);
    // `cout` should be just qualified with `std::` instead of
    // `using namespace std`. 
    std::cout<<max(i,len)<<"\n";
    return 0; 
} 
In silico
All true, but this isn't causing the compiler error in the question. Although I'm thoroughly puzzled how these sorts of typo errors can occur on SO... do people really re-type their code in the question box instead of just copy-pasting?
Tyler McHenry
Another thing that should be changed is the `int i;` declaration at the beginning of `max()` definition. The variable `i` is being declared locally within the `for` loop so the first declaration does nothing.
Praetorian
@Tyler, the case mismatch in `max` and `Max` is indeed what is causing the `error C2563: mismatch in formal parameter list` error; I just tried it on VS2008. Kinda strange, I'd have thought the error would be something along the lines of `Function max does not take 0 arguments` but with templates who knows ... I should probably be happy the error message didn't span 2 dozen lines :)What you said about people re-typing code in SO ... it does make you wonder doesn't it? Especially the mai() function instead of main()!
Praetorian
Yeah, that does make me wonder. I made a note of that.
In silico
A: 

if (data[i]>max) - change max to Max

naivnomore
A: 

In line 10, you probably mean >Max and not >max.

adamk
+3  A: 

Your max function is conflicting with the standard C++ function std::max because you have written using namespace std at the top of your program, which brings everything in the std namespace into the default namespace. The compiler can't tell if you wanted to call your max or some version of std::max.

Either change the name of your function, or remove using namespace std and explicitly prefix things in your program in the std namespace with std::.

Tyler McHenry
This is completely false! The most obvious reason is that std::max resides in <algorithm> and that header is not even included.Even if it is included, std::max takes two const references of type T, which is different than a pointer to T and int.When he writes max(i, len), compiler knows that i is of type int[] and len is of type int, thus overload resolution will know which max is being called.
ryaner
It's not false. Although he hasn't included <algorithm>, he has included <iostream>, and there's no guarantee that that won't include <algorithm> indirectly. Secondly, read the error message. It refers *explicitly* to `std::max` for two of the ambiguous overloads. Off the top of my head I'm not sure why it can't exclude the `std::max` overloads for the type-mismatch reasons you stated, but I suppose it would have to do with the order in which the compiler considers overload resolution vs. template instantiation. Either way, the error message is very clear about the issue being `std::max`.
Tyler McHenry
I apologize for my comment about <algorithm> header =) You were right indeed. I looked at the error message and realized that was caused at line 10. Which is his typo "max". In that case indeed the compiler would have no idea which address of the function it should get. You were completely right, actually, your answer explains the effect of his unintentional typo.
ryaner