Sorting a struct

J

J. Campbell

I'm having a problem sorting an array of structs that is a class
member. After doing a bit of research I thought I had my hands around
the problem. I've overloaded the '<' operator for the struct, and
then I pass sort a pointer to the first array element and the number
of elements. Everything compiles fine until I try to call std::sort()
on the array. I'm trying to make a class that acts like a dealer to
use in a card game, and I need to sort the cards as part of my shuffle
routine. I tried to post "the minimum compilable code", but it's
still a little long (circa 80 lines). Any help? Thanks. Joe
_________________________________
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs){
return index < rhs.index;
}
};

class deck{
private:
string theSuits; // contains suits in order
string theValues; // contains values in order
int numcards;
acard* card; // an array of cards

public:
deck();
~deck();
void show();
void shuffle();
};

deck::deck():
theSuits("\x05\x04\x03\x06") //c, d, h, s
,theValues("23456789TJKQA")
,numcards(theSuits.length() * theValues.length())
,card(new acard[numcards])
{
int cardnum(0);
for(int i = 0; i < theSuits.length(); ++i){
for(int j = 0; j < theValues.length(); ++j){
cardnum = (i * theValues.length()) + j;
card[cardnum].suit = i;
card[cardnum].value = j;
card[cardnum].str = theValues.substr(j,1) +
theSuits.substr(i,1); }
}
}

deck::~deck(){
delete [] card;

}

void deck::show(){
for(int i=0; i < numcards; ++i){
cout << (i % 13 ? ' ' : '\n') << card.str ;
}
cout << endl;
}

void deck::shuffle(){
for(int i=0; i < numcards; ++i){
card.index = rand();
}
//sort(card, card + numcards); // this line breaks the
compilability
} // but is needed to shuffle the
deck


int main()
{
srand(time(0));

deck mydeck;
mydeck.show();
mydeck.shuffle();
mydeck.show();

system("pause");
return 0;
}
___________________________________
 
G

Gianni Mariani

J. Campbell said:
I'm having a problem sorting an array of structs that is a class
member. After doing a bit of research I thought I had my hands around
the problem. I've overloaded the '<' operator for the struct, and
then I pass sort a pointer to the first array element and the number
of elements. Everything compiles fine until I try to call std::sort()
on the array. I'm trying to make a class that acts like a dealer to
use in a card game, and I need to sort the cards as part of my shuffle
routine. I tried to post "the minimum compilable code", but it's
still a little long (circa 80 lines). Any help? Thanks. Joe
_________________________________
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs){

replace the above line with:

bool operator <(const acard& rhs) const {
 
J

Joe C

Gianni Mariani said:
replace the above line with:

bool operator <(const acard& rhs) const {

Thanks Gianni. How *should* I have known that the comparitor function
*needed* to be const?
 
G

Gianni Mariani

Joe said:
Thanks Gianni. How *should* I have known that the comparitor function
*needed* to be const?

I found out because the compiler said so :

g++ xxcards.cpp -o xxcards
/include/c++/3.4.0/bits/stl_algo.h: In function `const _Tp&
std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = acard]':
/include/c++/3.4.0/bits/stl_algo.h:2484: instantiated from `void
std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator,
_Size) [with _RandomAccessIterator = acard*, _Size = int]'
/include/c++/3.4.0/bits/stl_algo.h:2555: instantiated from `void
std::sort(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = acard*]'
xxcards.cpp:67: instantiated from here
/include/c++/3.4.0/bits/stl_algo.h:90: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:91: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:93: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:97: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:99: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
 
Joined
Sep 1, 2006
Messages
1
Reaction score
0
Gianni Mariani said:
Joe C wrote:
> "Gianni Mariani" <[email protected]> wrote in message
> news:[email protected]...
>
>>J. Campbell wrote:
>>
>>>I'm having a problem sorting an array of structs that is a class
>>>member. After doing a bit of research I thought I had my hands around
>>>the problem. I've overloaded the '<' operator for the struct, and
>>>then I pass sort a pointer to the first array element and the number
>>>of elements. Everything compiles fine until I try to call std::sort()
>>>on the array. I'm trying to make a class that acts like a dealer to
>>>use in a card game, and I need to sort the cards as part of my shuffle
>>>routine. I tried to post "the minimum compilable code", but it's
>>>still a little long (circa 80 lines). Any help? Thanks. Joe
>>>_________________________________
>>>#include <algorithm>
>>>#include <iostream>
>>>#include <string>
>>>
>>>using namespace std;
>>>
>>>struct acard{
>>> public:
>>> int suit;
>>> int value;
>>> string str;
>>> unsigned int index;
>>> bool operator <(const acard& rhs){

>>
>>replace the above line with:
>>
>> bool operator <(const acard& rhs) const {

>
>
> Thanks Gianni. How *should* I have known that the comparitor function
> *needed* to be const?


I found out because the compiler said so :

g++ xxcards.cpp -o xxcards
/include/c++/3.4.0/bits/stl_algo.h: In function `const _Tp&
std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = acard]':
/include/c++/3.4.0/bits/stl_algo.h:2484: instantiated from `void
std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator,
_Size) [with _RandomAccessIterator = acard*, _Size = int]'
/include/c++/3.4.0/bits/stl_algo.h:2555: instantiated from `void
std::sort(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = acard*]'
xxcards.cpp:67: instantiated from here
/include/c++/3.4.0/bits/stl_algo.h:90: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:91: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:93: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:97: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:99: error: passing `const acard' as
`this' argument of `bool acard::eek:perator<(const acard&)' discards qualifiers

Thanks a lot Gianni. I found your reply quite useful in solving similar problem.
- Ravi
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top