By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Sorting a struct

P: n/a
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[i].str ;
}
cout << endl;
}

void deck::shuffle(){
for(int i=0; i < numcards; ++i){
card[i].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;
}
___________________________________
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 {
Jul 22 '05 #2

P: n/a

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cf********@dispatch.concentric.net...
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?

Jul 22 '05 #3

P: n/a
Joe C wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cf********@dispatch.concentric.net...
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::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:91: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:93: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:97: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:99: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.