Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiler errors: set with comparison function

Reply
Thread Tools

Compiler errors: set with comparison function

 
 
clark.coleman@att.net
Guest
Posts: n/a
 
      01-04-2008
I have a header file that compiles fine on Visual Studio .NET 2005 but
has big trouble on x86/Linux using gcc-4.1.1

I need to make sets of elements of type "op_t", which needs a complex
comparison function. Near the top of my header, I include the ehader
that defines "op_t", then I make this ordering function:

class LessOp {
public:
bool operator()(const op_t Opnd1, const op_t Opnd2) const {
if (Opnd1.type != Opnd2.type)
return (Opnd1.type < Opnd2.type);
switch (Opnd1.type) {

etc.
etc.

The first time I make reference to "LessOp" later in the header, I get
the compile errors from gcc but not Visual Studio. The reference is a
public member function of a class, declared as:

set<const op_t, LessOp>::iterator GetFirstLiveIn(void); // First
LiveIn ...

This is line 180 of SMPDataFlowAnalysis.h, as referenced in this gcc
error message:


/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/ext/
new_allocator.h: In instantiation of '__gnu_cxx::new_allocator<const
op_t>':
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/
bits/allocator.h:83: instantiated from 'std::allocator<const op_t>'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/
bits/stl_set.h:110: instantiated from 'std::set<const op_t, LessOp,
std::allocator<const op_t> >'
SMPDataFlowAnalysis.h:180: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/ext/
new_allocator.h:78: error: 'const _Tp*
__gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp =
const op_t]' cannot be overloaded
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/ext/
new_allocator.h:75: error: with '_Tp*
__gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const
op_t]'

I thought maybe I needed to make the parameters to LessOp's operator
be by reference, so I changed the declaration to be:

class LessOp {
public:
bool operator()(const op_t &Opnd1, const op_t &Opnd2) const {

This produces the exact same error message from gcc, and still no
errors or warnings from Visual Studio.

I have found numerous similar problems in this group and via search
engine, but almost all of them were the opposite problem of people not
using "const" whereas I used "const" all over the place.

Any help is appreciated.
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      01-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a header file that compiles fine on Visual Studio .NET 2005 but
> has big trouble on x86/Linux using gcc-4.1.1


> set<const op_t, LessOp>::iterator GetFirstLiveIn(void); // First

^^^^^
set< op_t, LessOp>....

set requires the type to be assignable.

As the implementation in vc2005, the allocator has a specialization of
allocator (actually Allocator_base as base class) for const type, which
simply removes the constness. Which I guess is an extension (not so
beautiful one).
 
Reply With Quote
 
 
 
 
clark.coleman@att.net
Guest
Posts: n/a
 
      01-04-2008
OK. It seems the problem that I had was that I switched between "const
everywhere" and "const nowhere" and kept getting mysterious errors
either way. The solution was to leave "const" on the arguments to the
comparison function and remove it everywhere else. That finally got
the errors to go away on both compilers.

Thanks.

 
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
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
how can i make a set with comparison function ? PicO C++ 14 08-11-2007 11:36 AM
Compiler Comparison (Dev-C++ vs. Visual C++) Geoff Hill C++ 2 02-11-2007 02:38 AM
Compiler standard compliance comparison Michael Andersson C++ 3 07-31-2003 01:11 PM



Advertisments