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;
}
___________________________________
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;
}
___________________________________