By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Error when using std::less

P: n/a
I get this error when I run my program:

red_black_tree.cpp:188: error: no matching function for call to
‘std::less<MyObj<int::less(MyObj<int>&, MyObj<int>&)’
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:224:
note: candidates are: std::less<MyObj<int::less()
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:224:
note: std::less<MyObj<int::less(const
std::less<MyObj<int&)
The code is rather comprehensive so I have just included the code that
causes the error below (where C is std::less as default):

/* used for testing. */
template <typename K, typename V, typename F, typename C, typename A,
typename N, bool B>
typename red_black_tree<K, V, F, C, A, N, B>::iterator
red_black_tree<K, V, F, C, A, N, B>::test_tree(value_type e1, value_type
e2) {

// this does not work!
if (C(e1,e2)) {
std::cout << "here\n";
}
return iterator(parent[(*this).header]);
}

This is the objects that I am trying to compare:

template<typename I>
class MyObj {
public:
MyObj(){}
MyObj(I a) : key(a) {}

I getKey() const {
return key;
}
void setKey(I a) {
key = a;
}

bool operator<(const MyObj& mobj) const {
return (key < mobj.getKey());
}
private:
I key;
};

int main() {

typedef red_black_tree<MyObjset;
set mySet;
MyObj<intmy1(1);
MyObj<intmy2(2);
mySet.test_tree(my1,my2);

return 0;
}

It seems that less can only be used like:

less()
or
less(const std::less<MyObj<int&)

But that is not how I use it or have read that it is supposed to be used.
Jun 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
desktop wrote:
I get this error when I run my program:

red_black_tree.cpp:188: error: no matching function for call to
‘std::less<MyObj<int::less(MyObj<int>&, MyObj<int>&)’
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:224:
note: candidates are: std::less<MyObj<int::less()
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:224:
note: std::less<MyObj<int::less(const
std::less<MyObj<int&)
The code is rather comprehensive so I have just included the code that
causes the error below (where C is std::less as default):

/* used for testing. */
template <typename K, typename V, typename F, typename C, typename A,
typename N, bool B>
typename red_black_tree<K, V, F, C, A, N, B>::iterator
red_black_tree<K, V, F, C, A, N, B>::test_tree(value_type e1, value_type
e2) {

// this does not work!
if (C(e1,e2)) {
Of course it doesn't. C is a type, so this would be a constructor call. Just
as the error message says. You need to hold a C object to call.
It seems that less can only be used like:

less()
or
less(const std::less<MyObj<int&)
Yes. This is the (presumably) compiler generated constructors. It is the
operator () that does the job.
>
But that is not how I use it or have read that it is supposed to be used.
Then you must use less differently, and read your books again.

--
Robert Bauck Hamar
Jun 10 '07 #2

P: n/a
On Jun 10, 8:32 pm, desktop <f...@sss.comwrote:
I get this error when I run my program:
red_black_tree.cpp:188: error: no matching function for call to
?std::less<MyObj<int::less(MyObj<int>&, MyObj<int>&)?
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:224:
note: candidates are: std::less<MyObj<int::less()
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:224:
note: std::less<MyObj<int::less(const
std::less<MyObj<int&)
The code is rather comprehensive so I have just included the code that
causes the error below (where C is std::less as default):
/* used for testing. */
template <typename K, typename V, typename F, typename C, typename A,
typename N, bool B>
typename red_black_tree<K, V, F, C, A, N, B>::iterator
red_black_tree<K, V, F, C, A, N, B>::test_tree(value_type e1, value_type
e2) {
// this does not work!
if (C(e1,e2)) {
C is a type, so C(e1,e2) is (formally) a function style type
conversion, or (more usually) an explicit call of the
constructor. You create a new, temporary instance of whatever C
is, which is then converted to bool. Except, of course, that if
C doesn't have an appropriate constructor, or doesn't have an
implicit conversion to bool, you'll get a compiler error.

If std::less is an acceptable argument for C, you want:

if ( C()( e1, e2 ) ) {

More likely, however, you'd want a member of the class with type
C, and use it. This will also allow passing specific instances
of the class to the constructor, and using them to instantiate
the class member, something that is necessary if, like the
standard classes, you want to support pointers to functions as
well.
It seems that less can only be used like:
less()
or
less(const std::less<MyObj<int&)
Those are the only two constructors for std::less.
But that is not how I use it or have read that it is supposed
to be used.
I think you're confusing type and object. std::less is a type.
The name of a type can be used to create or declare objects of
that type, but that's about it. What you want is an object of
type std::less, on which you can call member functions,
and---because std::less has an overridden operator()()---you can
use as if it were a function.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.