Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Get a C++ set to contain objects

Reply
Thread Tools

Get a C++ set to contain objects

 
 
janzon@gmail.com
Guest
Posts: n/a
 
      09-30-2006
Hi!

I'm a Java guy, forced to do some C++. I want to add objects to the set
container. I don't even get past the compilation step (which is a good
thing in a way). For instance, why doesn't the following code compile?

#include <iostream>
#include <set>

using namespace std;

class myClass {
public:
void hello() { cout << "hello"; }
};

int main() {
myClass mc;

set<myClass> s;
s.insert(mc); // If this line is commented away, no error
}


The compiles gives as a reason the following:

bik> CC -o prog4 prog4.cc
"/opt/SUNWspro/prod/include/CC/Cstd/./functional", line 166: Error: The
operation "const myClass<const myClass" is illegal.
"/opt/SUNWspro/prod/include/CC/Cstd/rw/tree.cc", line 177: Where:
While instantiating "std::less<myClass>:perator()(const myClass&,
const myClass&) const".
"/opt/SUNWspro/prod/include/CC/Cstd/rw/tree.cc", line 177: Where:
Instantiated from __rwstd::__rb_tree<myClass, myClass,
__rwstd::__ident<myClass, myClass>, std::less<myClass>,
std::allocator<myClass>>::insert(const myClass&).
"/opt/SUNWspro/prod/include/CC/Cstd/./set", line 224: Where:
Instantiated from non-template code.
1 Error(s) detected.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-30-2006
wrote:

> Hi!
>
> I'm a Java guy, forced to do some C++. I want to add objects to the set
> container. I don't even get past the compilation step (which is a good
> thing in a way). For instance, why doesn't the following code compile?
>
> #include <iostream>
> #include <set>
>
> using namespace std;
>
> class myClass {
> public:
> void hello() { cout << "hello"; }
> };
>
> int main() {
> myClass mc;
>
> set<myClass> s;
> s.insert(mc); // If this line is commented away, no error
> }

[snip]

The std::set<> container keeps its elements in order. For this to happen,
you need to supply a comparison predicate. Technically, you have the
following options:

a) Define an operator< for myClass. This can be either a member operator or
a freestanding friend. This solution conveys the message that the objects
of type myClass a naturally comparable.

b) Specialize std::less<myClass>. This indicates that the ordering you
define is less natural and more of a hack to make things like std::set and
std::map work.

c) Define a freestanding comparison function

bool some_function_name ( myClass const &, myClass const & );

and pass it as a parameter upon construction of the set. Generally, I would
not recommend this since you will have to supply additional template
parameters and it makes the code less readable. This, I would use only when
there are several sorting criteria for the same type that I need to use
independently in the code.


In any case, the comparison predicate you supply has to define a ordering,
i.e., you may not have things like a < b < c < a and you should have either
a<b or b<a or a==b. If these conditions are not met, the code is likely to
compiler but it is also likely to give unexpected results (aka bugs).


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      09-30-2006
wrote:

> int main() {
> myClass mc;
>
> set<myClass> s;
> s.insert(mc); // If this line is commented away, no error
> }


std::set is ordered, then it needs a way to compare things (and also to
evaluate equality). You need a < operator for objects of your class, or an
specialization for him of the std::less template, or add to the definition
of your set a compare operation.

--
Salu2
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      09-30-2006
wrote:

> Hi!
>
> I'm a Java guy, forced to do some C++. I want to add objects to the set
> container. I don't even get past the compilation step (which is a good
> thing in a way). For instance, why doesn't the following code compile?
>
> #include <iostream>
> #include <set>
>
> using namespace std;
>
> class myClass {
> public:
> void hello() { cout << "hello"; }
> };
>
> int main() {
> myClass mc;
>
> set<myClass> s;
> s.insert(mc); // If this line is commented away, no error
> }


Note, although the above compiles in Java, it wouldn't work because the
set (TreeMap in Java) has no way to compare two myClass objects to see
what the order should be.

> The compiles gives as a reason the following:
>
> bik> CC -o prog4 prog4.cc
> "/opt/SUNWspro/prod/include/CC/Cstd/./functional", line 166: Error: The
> operation "const myClass<const myClass" is illegal.
> "/opt/SUNWspro/prod/include/CC/Cstd/rw/tree.cc", line 177: Where:
> While instantiating "std::less<myClass>:perator()(const myClass&,
> const myClass&) const".


The above complains about the very thing that would cause this code to
fail in Java. No way to compare two myClass objects to determine order.

To do that in C++, you must provide a global function that returns true
of the left hand argument comes before the right hand argument.

bool compare( const myClass& lhs, const myClass& rhs ) {
// return true if lhs should come before rhs, otherwise return false.
}

If you name your compare function "operator<" then the set will just
work. If you want to name it something else, then you will need to let
set know.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
lw1a2
Guest
Posts: n/a
 
      09-30-2006

wrote:
> Hi!
>
> I'm a Java guy, forced to do some C++. I want to add objects to the set
> container. I don't even get past the compilation step (which is a good
> thing in a way). For instance, why doesn't the following code compile?
>
> #include <iostream>
> #include <set>
>
> using namespace std;
>
> class myClass {
> public:
> void hello() { cout << "hello"; }
> };
>
> int main() {
> myClass mc;
>
> set<myClass> s;
> s.insert(mc); // If this line is commented away, no error
> }
>
>
> The compiles gives as a reason the following:
>
> bik> CC -o prog4 prog4.cc
> "/opt/SUNWspro/prod/include/CC/Cstd/./functional", line 166: Error: The
> operation "const myClass<const myClass" is illegal.
> "/opt/SUNWspro/prod/include/CC/Cstd/rw/tree.cc", line 177: Where:
> While instantiating "std::less<myClass>:perator()(const myClass&,
> const myClass&) const".
> "/opt/SUNWspro/prod/include/CC/Cstd/rw/tree.cc", line 177: Where:
> Instantiated from __rwstd::__rb_tree<myClass, myClass,
> __rwstd::__ident<myClass, myClass>, std::less<myClass>,
> std::allocator<myClass>>::insert(const myClass&).
> "/opt/SUNWspro/prod/include/CC/Cstd/./set", line 224: Where:
> Instantiated from non-template code.
> 1 Error(s) detected.


You must overload myClass's operator <
Like this:

#include <iostream>
#include <set>
using namespace std;


class myClass {
public:
myClass():data(0){}
void hello() { cout << "hello"; }
bool operator<(const myClass& rhs) const
{
return data<rhs.data;
}
private:
int data;//This is a comparative data.
};


int main() {
myClass mc;

set<myClass> s;
s.insert(mc); // If this line is commented away, no error



}

 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      10-01-2006
"Daniel T." <> wrote in message
news:daniel_t-...
> wrote:
>
>> Hi!
>>
>> I'm a Java guy, forced to do some C++. I want to add objects to the set
>> container. I don't even get past the compilation step (which is a good
>> thing in a way). For instance, why doesn't the following code compile?
>>
>> #include <iostream>
>> #include <set>
>>
>> using namespace std;
>>
>> class myClass {
>> public:
>> void hello() { cout << "hello"; }
>> };
>>
>> int main() {
>> myClass mc;
>>
>> set<myClass> s;
>> s.insert(mc); // If this line is commented away, no error
>> }

>
> Note, although the above compiles in Java, it wouldn't work because the
> set (TreeMap in Java) has no way to compare two myClass objects to see
> what the order should be.


TreeSet surely?

<snip>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to delete vectors that contain pointers to user-defined type objects dwightarmyofchampions@hotmail.com C++ 16 07-09-2012 08:07 AM
RCR: String#contain? and Array#contain? Roger Pack Ruby 3 09-28-2010 04:13 PM
Does string contain A, and if so, does a section of string contain B Jason Carlton Javascript 11 12-08-2009 06:07 PM
Deleting elements of vectors that contain pointers to other objects dwightarmyofchampions@hotmail.com C++ 7 03-20-2009 09:06 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57