"hall" <Xc***********@yahoo.se> wrote:
I accidently overloaded a static member function that I use as predicate
in the std::sort() for a vector and ended up with a compiler error. Is
this kind of overload not allowed for predicates and if so, why not?
Shouldn the compiler be able to tell which of he overloaded functions to
use?
The second A::comp() is the one I accidently added and gives the error
message (in Borland C++Builder 6)
[C++ Error] Unit1.cpp E2285 Could not find a match for
'sort<_RandomAccesIter, _Compare>(A*,A*,bool(*)(const A&,const A&))'
Commenting out the line makes the code compile just fine.
//---------------------------------------------------------------------------
#include <vector>
#include <algorithm>
using namespace std;
struct A {
int a;
A(int a0): a(a0) {}
static bool comp ( const A a1, const A & a2) {return a1.a<a2.a;}
static bool comp ( const A a1 ){ return true; } //< causes
compiler error
};
int main(int argc, char* argv[])
{
vector<A> a;
a.push_back(A(4));
a.push_back(A(7));
sort( a.begin(), a.end(), A::comp );
return 0;
}
Well, one thing that bothers me is that sort() expects the
predicate to have argument signature:
(const A&, const A&)
but you use:
(const A , const A&)
Forget the '&'?
That may not be related to your problem, but it's
an error you should look into.
I did try compiling your program on DJGPP (a port of the
Gnu C++ compiler to Windows-command-prompt environment,
from
www.delorie.com ). I got this:
wd=C:\C\test
%make predicate-test
gpp -IC:/C/lib -pedantic -Wall -W -Wshadow -Wcast-qual -Wcast-align -Wconversion
-Os -s -LC:/C/lib predicate-test.cpp -lrh -lm -o C:/Software/predicate-test.exe
predicate-test.cpp: In function `int main()':
predicate-test.cpp:19: error: no matching function for call to `sort(
__gnu_cxx::__normal_iterator<A*, std::vector<A, std::allocator<A> > >,
__gnu_cxx::__normal_iterator<A*, std::vector<A, std::allocator<A> > >,
<unknown type>)'
make.exe: *** [predicate-test] Error 1
But when I comment-out the extra version of comp, it compiles fine.
I get the same results if I take the comp's out of the class and
make them global functions. I even changed the extra comp to
a totally unrelated signature:
double comp (int a, char b)
{
return a + b;
}
But any way I try it, the extra comp apparently causes
the compiler to see "comp" in your call to sort() as being
an "unknown type".
Perhaps the problem is related to the fact that the name "comp"
is a pointer to a function. Perhaps sort() can't check signatures
and relies on an unambiguous pointer-to-function called "comp",
which would preclude overloading.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---