Compiler errors: set with comparison function

Discussion in 'C++' started by clark.coleman@att.net, Jan 4, 2008.

  1. Guest

    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.
    , Jan 4, 2008
    #1
    1. Advertising

  2. Barry Guest

    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).
    Barry, Jan 4, 2008
    #2
    1. Advertising

  3. Guest

    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.
    , Jan 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    942
    Mark Goldin
    Jan 17, 2004
  2. Michael Andersson

    Compiler standard compliance comparison

    Michael Andersson, Jul 31, 2003, in forum: C++
    Replies:
    3
    Views:
    424
    Greg P.
    Jul 31, 2003
  3. Geoff Hill
    Replies:
    2
    Views:
    602
    Victor Bazarov
    Feb 11, 2007
  4. PicO
    Replies:
    14
    Views:
    609
    James Kanze
    Aug 11, 2007
  5. Deepu
    Replies:
    1
    Views:
    238
    ccc31807
    Feb 7, 2011
Loading...

Share This Page