public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32851]  New: Sort of vector of template class fails
@ 2007-07-22  8:30 klra67 at freenet dot de
  2007-07-22  8:33 ` [Bug c++/32851] " klra67 at freenet dot de
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: klra67 at freenet dot de @ 2007-07-22  8:30 UTC (permalink / raw)
  To: gcc-bugs

Sorting a vector comprised of a class defined as a template fails.

-----------------
#include <vector>
#include <algorithm>

class card {
public:
    int id;
    bool operator<(card const& A) { return this->id < A.id;};
    card() {id=0;}
};

template <class MyCard>
class cards {
protected:
    std::vector<MyCard> cardvec;

public:
    void sort(void){std::sort(cardvec.begin(), cardvec.end());}
    cards(){ card aCard ; cardvec.insert(cardvec.end(), aCard);}
};

int main()
{
    cards<card> *mycards;
    mycards = new cards<card>;
    mycards->sort();
}


-- 
           Summary: Sort of vector of template class fails
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: klra67 at freenet dot de
 GCC build triplet: i686-linuc-gcc
  GCC host triplet: i686-linuc-gcc
GCC target triplet: i686-linuc-gcc


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32851


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/32851] Sort of vector of template class fails
  2007-07-22  8:30 [Bug c++/32851] New: Sort of vector of template class fails klra67 at freenet dot de
@ 2007-07-22  8:33 ` klra67 at freenet dot de
  2007-07-23 21:55 ` [Bug libstdc++/32851] " pinskia at gcc dot gnu dot org
  2007-07-24 16:10 ` klra67 at freenet dot de
  2 siblings, 0 replies; 4+ messages in thread
From: klra67 at freenet dot de @ 2007-07-22  8:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from klra67 at freenet dot de  2007-07-22 08:32 -------
Created an attachment (id=13946)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=13946&action=view)
Minimum test case

A minimum test case to reproduce the error. Code compiles well on another
compiler.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32851


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/32851] Sort of vector of template class fails
  2007-07-22  8:30 [Bug c++/32851] New: Sort of vector of template class fails klra67 at freenet dot de
  2007-07-22  8:33 ` [Bug c++/32851] " klra67 at freenet dot de
@ 2007-07-23 21:55 ` pinskia at gcc dot gnu dot org
  2007-07-24 16:10 ` klra67 at freenet dot de
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-07-23 21:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2007-07-23 21:55 -------
The error message is:
/home/apinski/x86-local-fsf/lib/gcc/i686-pc-linux-gnu/4.3.0/../../../../include/c++/4.3.0/bits/stl_algo.h:2789:
  instantiated from 'void std::sort(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator =
__gnu_cxx::__normal_iterator<card*, std::vector<card, std::allocator<card> >
>]'
t1212.cc:17:   instantiated from 'void cards<MyCard>::sort() [with MyCard =
card]'
t1212.cc:25:   instantiated from here
/home/apinski/x86-local-fsf/lib/gcc/i686-pc-linux-gnu/4.3.0/../../../../include/c++/4.3.0/bits/stl_algo.h:92:
error: passing 'const card' as 'this' argument of 'bool card::operator<(const
card&)' discards qualifiers


This is correct, you need to mark operator< as taking a const this variable.
Like:
    bool operator<(const card & A)const { return this->id < A.id;};

Once doing that, the program compiles.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
          Component|c++                         |libstdc++
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32851


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/32851] Sort of vector of template class fails
  2007-07-22  8:30 [Bug c++/32851] New: Sort of vector of template class fails klra67 at freenet dot de
  2007-07-22  8:33 ` [Bug c++/32851] " klra67 at freenet dot de
  2007-07-23 21:55 ` [Bug libstdc++/32851] " pinskia at gcc dot gnu dot org
@ 2007-07-24 16:10 ` klra67 at freenet dot de
  2 siblings, 0 replies; 4+ messages in thread
From: klra67 at freenet dot de @ 2007-07-24 16:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from klra67 at freenet dot de  2007-07-24 16:10 -------
Subject: Re:  Sort of vector of template class fails

Am Montag, 23. Juli 2007 23:55 schrieb pinskia at gcc dot gnu dot org:

> This is correct, you need to mark operator< as taking a const this
> variable. Like:
>     bool operator<(const card & A)const { return this->id < A.id;};
>
> Once doing that, the program compiles.
You are right, sorry. I am not a C++ expert, and this worked without const on 
another compiler, so I got confused. BTW, I find the error message unclear 
and not helpful, even now that I understand what is going on.

Thanks!
klaus


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32851


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-07-24 16:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-22  8:30 [Bug c++/32851] New: Sort of vector of template class fails klra67 at freenet dot de
2007-07-22  8:33 ` [Bug c++/32851] " klra67 at freenet dot de
2007-07-23 21:55 ` [Bug libstdc++/32851] " pinskia at gcc dot gnu dot org
2007-07-24 16:10 ` klra67 at freenet dot de

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).