fts
fts

Reputation: 141

C++ Function Template Error

I have just started to use the function templates in c++. I am using these tutorials. I am trying to implement a basic code which goes something like this.

#include<iostream>
using namespace std;

template<class t>
t max(t a,t b){
    t max_value;
    max_value = (a>b)?a:b;
    return max_value;
}

int main(){
    int a=9,b=8,c;
    c=max<int>(a,b);
    cout<<c;
    return 0;
}

However I get the following error.

/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/gursheel/NetBeansProjects/project_q7'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/project_q7
gmake[2]: Entering directory `/home/gursheel/NetBeansProjects/project_q7'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:16:19: error: call of overloaded ‘max(int&, int&)’ is ambiguous
main.cpp:16:19: note: candidates are:
main.cpp:4:3: note: t max(t, t) [with t = int]
In file included from /usr/include/c++/4.7/bits/char_traits.h:41:0,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from main.cpp:1:
/usr/include/c++/4.7/bits/stl_algobase.h:210:5: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
gmake[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
gmake[2]: Leaving directory `/home/gursheel/NetBeansProjects/project_q7'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/gursheel/NetBeansProjects/project_q7'
gmake: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 318ms)

I am not able to understand what the error exactly is. Any help would be appreciated.

Upvotes: 1

Views: 274

Answers (3)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158449

You need to remove:

using namespace std;

you are clashing with std::max. This is one of the reasons Why “using namespace std;” is considered bad practice and this is the C++ FAQs take on it. Typing std::cout is really not that bad and you will get used to adding std:: pretty quickly and it will just save you trouble in the long run.

Upvotes: 4

taocp
taocp

Reputation: 23624

Another way is to add :: when you call your max function template:

 c=::max<int>(a,b);

This will tell the compiler to find the max function template in the global namespace. In this case, your version of max will be used. You may find a live demo here: Demo

Upvotes: 1

mshriv
mshriv

Reputation: 322

The issue here is that the "max" template function that you have defined is conflicting with max function that is part of STL. Both your and STL's have same signature and both are template functions. STL's max is inside namespace "std". But since you have specified "using namespace std" in your code STL's max has become visible. There are 3 ways i can think of to avoid this situation

1) Rename your max as max1 or some other name

2) Comment "using namespace std" and replace cout with std::cout

3) Replace following

c=max<int>(a,b);

with

c=::max<int>(a,b);

Prepending :: to a function call tells the compiler to pick the function from global namespace. Your function is defined in global namespace.

Upvotes: 2

Related Questions