473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

std::less and pointers?

Hello

How can I define an operator for such:

bool operator<(my_class *l,my_class *r)
{
return l->_attribute < r->_attribute;
}

So that I can use with std::less, as:

std::set<my_class *, std::less<my_class*> > set1_t;

In VC++ compiler I get this error:
error C2803: 'operator <' must have at least one formal parameter of class
type.

It needs me to define the operator as:

bool operator<(my_class &l,my_class &r)

(It require me not to use pointers)

Please advise.

There is another solution is to write the comparator class and overload the
() operator.

Any other solutions?

--
Elias
Mar 28 '06 #1
5 10905
Hi

lallous wrote:
How can I define an operator for such:

bool operator<(my_class *l,my_class *r)
You cannot overload operators for non-user-defined types.
{
return l->_attribute < r->_attribute;
}
And this wouldn't work properly anyway. The comparisons for pointers are
only defined when both pointers point to the same object, or to objects
from the same array.

To compare two distinct and unrelated pointers, you _must_ use std::less.
So that I can use with std::less, as:

std::set<my_class *, std::less<my_class*> > set1_t;
Why don't you just give a different comparison functor?
In VC++ compiler I get this error:
error C2803: 'operator <' must have at least one formal parameter of class
type.
Your compiler is correct.
It needs me to define the operator as:

bool operator<(my_class &l,my_class &r)
Which is a completely different thing...
There is another solution is to write the comparator class and overload
the () operator.
It's the natural and correct way.
Any other solutions?


I don't think so.
Why do you need one?

Markus
Mar 28 '06 #2
Markus Moll wrote:
return l->_attribute < r->_attribute;
}


And this wouldn't work properly anyway. The comparisons for pointers are
only defined when both pointers point to the same object, or to objects
from the same array.


Presumably _attribute is NOT a pointer, but some type where
< is well defined between different objects.
Mar 28 '06 #3
Hi

Ron Natalie wrote:
Presumably _attribute is NOT a pointer, but some type where
< is well defined between different objects.


D'oh... I missed that part... :-/ sorry for the confusion.

Markus
Mar 28 '06 #4
If you're trying to do this because you want to use your objects in a
sorted container, or you want to sort a container, then consider using
a smart pointer that has value semantics for the comparison operators.
Check out the following smart pointer:
http://axter.com/smartptr

You can use the above smart pointer in a sorted container, without
having to create an operator for the pointers.
std::set<smart_ptr<my_class> > myset;
std::map<smart_ptr<my_class>, int > mymap;

----------------------------------------------------------------------------------------
David Maisonave
http://axter.com

Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------

Mar 29 '06 #5
Hello Markus

"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.de> wrote in message
news:ne********************@luna.igd.fhg.de...
Hi

lallous wrote:
How can I define an operator for such:

bool operator<(my_class *l,my_class *r)
You cannot overload operators for non-user-defined types.


So if I typedef my_class * myclass_p , then write the operator using
myclass_p then the compiler won't complain?
(I defined my user type)

And this wouldn't work properly anyway. The comparisons for pointers are
only defined when both pointers point to the same object, or to objects
from the same array.

To compare two distinct and unrelated pointers, you _must_ use std::less.


Sorry how do you mean it?

So that I can use with std::less, as:

std::set<my_class *, std::less<my_class*> > set1_t;


Why don't you just give a different comparison functor?

There is another solution is to write the comparator class and overload
the () operator.


It's the natural and correct way.
Any other solutions?


I don't think so.
Why do you need one?


I didn't want to write another class, instead I just wanted to overload an
operator and use std::less

--
Elias
Apr 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Exits Funnel | last post by:
I've got this code: //Begin foo.cpp #include <utility> #include <bits/stl_function.h> typedef std::pair<int, int> int_pair_t; template<> struct std::less<int_pair_t> { };
5
by: Vinu | last post by:
Hi I am facing a problem in compilation the error is like this In constructor xServices::CServices<TImp>::StHoldClientList::StHoldClientList(std::set<TImp*, std::less<TImp*>,...
16
by: Cory Nelson | last post by:
Does anyone know how std::set prevents duplicates using only std::less? I've tried looking through a couple of the STL implementations and their code is pretty unreadable (to allow for different...
3
by: ek | last post by:
I have the following class: template<typename I> class test { public: test(I i) : pp(i) {} I getpp() const { return pp; }
2
by: desktop | last post by:
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>&)’...
0
by: Juha Nieminen | last post by:
Mike Copeland wrote: Regardless of what ChipRecord is, that map will have an unambiguous ordering because the operator<() for std::string will be used. This is an iterator to a completely...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.