tags:

views:

148

answers:

5

I am new to templates in c++. i was trying some small programs.

    CPP [80]> cat 000001.cpp 000001.hpp
#include <iostream>
#include <string>
#include "000001.hpp"

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << ::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}

template <typename T>
inline T const& max (T const& a, T const& b)
{
        return  a < b ? b : a;
}

when i compile this program:

i get an error below:

    CPP [78]> /opt/aCC/bin/aCC -AA 000001.cpp
Error (future) 229: "/opt/aCC/include_std/string.cc", line 164 # "Ambiguous overloaded function call; a
    function match was not found that was strictly best for ALL arguments. Two functions that matched
    best for some arguments (but not all) were "const unsigned long &max<unsigned long>(const unsigned
    long &,const unsigned long &)" ["000001.hpp", line 2] and "const unsigned long &std::max<unsigned
    long>(const unsigned long &,const unsigned long &)" ["/opt/aCC/include_std/algorithm", line 1762]."
    Choosing "const unsigned long &max<unsigned long>(const unsigned long &,const unsigned long &)"
    ["000001.hpp", line 2] for resolving ambiguity.
            _C_data = _C_getRep (max (_RW::__rw_new_capacity (0, this),
                                 ^^^
Warning:        1 future errors were detected and ignored. Add a '+p' option to detect and fix them before they become fatal errors in a future release. Behavior of this ill-formed program is not guaranteed to match that of a well-formed program

Could nybody please tell me what exactly the error is?

+1  A: 

The code you've posted compiles just fine, there must be something else that is wrong inside "000001.hpp". Can you post the contents of that file too?

Edit: If you do as avakar says but the problem persists, that must be due to some problem with your compiler. There are two obvious workarounds I can think of: rename your max function to something else, or put it in a namespace:

namespace Foo
{
    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        return  a < b ? b : a;
    }
}

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << Foo::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << Foo::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << Foo::max(s1,s2) << std::endl;
}
Manuel
this workaround works.but i kept the scope resolution operator infront of max so that it should not call std::max.even then why the error is coming?
cpp_Beginner
The reason is the one described by avakar. If it is true that your "000001.hpp" header contains only the definition of `max`, then the problem is with your compiler, which is injecting some std symbols in the global namespace when it shouldn't.
Manuel
+8  A: 

You are probably including <iostream.h> instead of <iostream> somewhere. The former hasn't existed for some time now, but for compatibility reasons, you compiler still accepts the include and replaces it with

#include <iostream>
using namespace std;

This causes std::max to be brought to the global namespace, thus resulting in an ambiguity. Replace <iostream.h> with <iostream> or rename your max function and the problem should disappear.

Edit: You've apparently fixed the include, but I bet you still have using namespace std; somewhere. You need to get rid of that. In fact you should never use using namespace in the global scope.

Edit: You might also have using std::max somewhere. You need to get rid of it too.

avakar
That's not his problem, the code compiles fine even if you add `using namespace std` (tested on VS2008 and GCC 4.1).
Manuel
Not if you also include <algorithm> that defines `std::max`. If you read the error report in detail you will find that the compiler is having trouble dealing with the ambiguity of `max` defined in the user header and `max` defined in the `algorithm` header (precisely in line 1762).
David Rodríguez - dribeas
Still, that should not be a bug, should it? The name `::max` is not reserved in the standard.
MSalters
@Manuel, driberas is correct, OP's probably including something that includes `<algorithm>` as well.
avakar
@MSalters, it is not reserved, but if you bring `std::max` to scope and define your own, you'll get the ambiguity.
avakar
I tried in GCC 4.1 and VS2008 with `#include <algorithm>` and `using namespace std` and the code works fine. So maybe he's using some other compiler.
Manuel
the compiler i am using is aCC
cpp_Beginner
@Manuel, indeed, it stops working when you change `::max` to `max`. I'll have to look into that. (I didn't know it makes any difference. Apparently, OP's compiler didn't either :) )
avakar
@avakar - It's even more interesting than that: it also stops working if you use `using std::max` instead of `using namespace std`.
Manuel
@Manuel, true, I got the same behavior on msvc9. I wish I had time right now to browse the standard.
avakar
It looks like the string implementation is making an unqualified call to `max`? Isn't it a bit of a glitch in the implementation (if it is meant to call `std::max`, perhaps it should say so)?
visitor
@avakar: please note that the error occurs in an aCC header. Sure, I can create an ambiguity by bringing `std::max` into the global namespace, but that should never break code in the `std` namespace.
MSalters
+1  A: 

I don't know which compiler you are using but the second error tells you that the two following functions are clashing :

::max
std::max

It seems really weird, you may have a using namespace std; somewhere or worse, that one of your include use iostream.h as noted in the first error. Could you give more information about your compiler/toolchain and the content of your .hpp file?

Raoul Supercopter
A: 

It says that two definitions for

max<unsigned long>

were found. One definition is in 000001.hpp and the other is in /opt/aCC/include_std/algorithm. The compiler chose the one in 000001.hpp for now, so no error is present now. But it says that these two definitions may cause errors in the future.

erelender
A: 

I don't know if this causes the problem, but anyhow; you shouldnt use the name max for your global (or local) function as it is a part of STL.

Viktor Sehr