overloading error for C++ template function
up vote
0
down vote
favorite
I am trying to do some practice with function templates as in the following example:
#include <iostream>
using namespace std;
template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}
int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;
retun 0;
}
But I got the following errors. Could anybody recognize where the problem is?
..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)
I am sorry I am new to templates. Thanks for your help.
c++ eclipse templates
add a comment |
up vote
0
down vote
favorite
I am trying to do some practice with function templates as in the following example:
#include <iostream>
using namespace std;
template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}
int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;
retun 0;
}
But I got the following errors. Could anybody recognize where the problem is?
..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)
I am sorry I am new to templates. Thanks for your help.
c++ eclipse templates
max returns bool not T
– michelson
Nov 10 at 18:17
1
Get rid ofusing namespace std;
and you won't clash with std::max.
– Jesper Juhl
Nov 10 at 18:24
@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26
1
Or stop naming your functionsmax
ormin
, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
– PaulMcKenzie
Nov 10 at 18:28
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to do some practice with function templates as in the following example:
#include <iostream>
using namespace std;
template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}
int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;
retun 0;
}
But I got the following errors. Could anybody recognize where the problem is?
..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)
I am sorry I am new to templates. Thanks for your help.
c++ eclipse templates
I am trying to do some practice with function templates as in the following example:
#include <iostream>
using namespace std;
template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}
int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;
retun 0;
}
But I got the following errors. Could anybody recognize where the problem is?
..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)
I am sorry I am new to templates. Thanks for your help.
c++ eclipse templates
c++ eclipse templates
asked Nov 10 at 18:14
Spring19981
73
73
max returns bool not T
– michelson
Nov 10 at 18:17
1
Get rid ofusing namespace std;
and you won't clash with std::max.
– Jesper Juhl
Nov 10 at 18:24
@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26
1
Or stop naming your functionsmax
ormin
, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
– PaulMcKenzie
Nov 10 at 18:28
add a comment |
max returns bool not T
– michelson
Nov 10 at 18:17
1
Get rid ofusing namespace std;
and you won't clash with std::max.
– Jesper Juhl
Nov 10 at 18:24
@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26
1
Or stop naming your functionsmax
ormin
, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
– PaulMcKenzie
Nov 10 at 18:28
max returns bool not T
– michelson
Nov 10 at 18:17
max returns bool not T
– michelson
Nov 10 at 18:17
1
1
Get rid of
using namespace std;
and you won't clash with std::max.– Jesper Juhl
Nov 10 at 18:24
Get rid of
using namespace std;
and you won't clash with std::max.– Jesper Juhl
Nov 10 at 18:24
@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26
@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26
1
1
Or stop naming your functions
max
or min
, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.– PaulMcKenzie
Nov 10 at 18:28
Or stop naming your functions
max
or min
, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.– PaulMcKenzie
Nov 10 at 18:28
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
Your usage of templates is correct, but the compiler complains that there already is a function called max
with same arguments.
It's full name would be std::max
, but because you wrote using namespace std
its just max
and compiler cannot know which function to call.
Solution is not to use using
, see Why is "using namespace std" considered bad practice? .
add a comment |
up vote
1
down vote
using namespace std;
is the issue
Please stop using that, see why
The iostream header includes another header file that pulls std::max
, giving a compiler error.
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Your usage of templates is correct, but the compiler complains that there already is a function called max
with same arguments.
It's full name would be std::max
, but because you wrote using namespace std
its just max
and compiler cannot know which function to call.
Solution is not to use using
, see Why is "using namespace std" considered bad practice? .
add a comment |
up vote
4
down vote
Your usage of templates is correct, but the compiler complains that there already is a function called max
with same arguments.
It's full name would be std::max
, but because you wrote using namespace std
its just max
and compiler cannot know which function to call.
Solution is not to use using
, see Why is "using namespace std" considered bad practice? .
add a comment |
up vote
4
down vote
up vote
4
down vote
Your usage of templates is correct, but the compiler complains that there already is a function called max
with same arguments.
It's full name would be std::max
, but because you wrote using namespace std
its just max
and compiler cannot know which function to call.
Solution is not to use using
, see Why is "using namespace std" considered bad practice? .
Your usage of templates is correct, but the compiler complains that there already is a function called max
with same arguments.
It's full name would be std::max
, but because you wrote using namespace std
its just max
and compiler cannot know which function to call.
Solution is not to use using
, see Why is "using namespace std" considered bad practice? .
answered Nov 10 at 18:19
Quimby
52719
52719
add a comment |
add a comment |
up vote
1
down vote
using namespace std;
is the issue
Please stop using that, see why
The iostream header includes another header file that pulls std::max
, giving a compiler error.
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
add a comment |
up vote
1
down vote
using namespace std;
is the issue
Please stop using that, see why
The iostream header includes another header file that pulls std::max
, giving a compiler error.
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
add a comment |
up vote
1
down vote
up vote
1
down vote
using namespace std;
is the issue
Please stop using that, see why
The iostream header includes another header file that pulls std::max
, giving a compiler error.
using namespace std;
is the issue
Please stop using that, see why
The iostream header includes another header file that pulls std::max
, giving a compiler error.
answered Nov 10 at 18:19
P0W
32.6k74692
32.6k74692
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
add a comment |
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
Thanks for everybody's help. My problem has been solved.
– Spring19981
Nov 10 at 18:34
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241988%2foverloading-error-for-c-template-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
max returns bool not T
– michelson
Nov 10 at 18:17
1
Get rid of
using namespace std;
and you won't clash with std::max.– Jesper Juhl
Nov 10 at 18:24
@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26
1
Or stop naming your functions
max
ormin
, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.– PaulMcKenzie
Nov 10 at 18:28