   Card and Deck shuffle problem
  Deck.cpp#include "Deck.h"// create a new deck consisting of 1 deck of cardsDeck::Deck() : numOfDecks(1){aDeck.resize(52);vector<Card>::size_type index = 0;for (int f = 1; f <= 13; ++f){for (int s = 1; s <= 4; ++s){aDeck[index] = Card(f, s);++index;}}}// create a new deck consisting of n decks of cardsDeck::Deck(int n) : numOfDecks(n){aDeck.resize(52 * numOfDecks);vector<Card>::size_type index = 0;for(int i = 0; i < n; ++i){for (int f = 1; f <= 13; ++f){for (int s = 1; s <= 4; ++s){aDeck[index] = Card(f, s);++index;}}}}Deck::Deck(const Deck& deck){// copy constructor}// shuffle deckconst void Deck::shuffleDeck(void) const{vector<Card>::size_type swapIndex;vector<Card>::const_iterator index;for(index = aDeck.begin(); index != aDeck.end(); ++index)    {swapIndex = rand() % 52;Card temp = aDeck[swapIndex];aDeck[swapIndex] = index;index = temp;    }cout << "-----------------------------------\n";cout << "\tDeck Shuffled\n";cout << "-----------------------------------\n";}// display info about deckconst void Deck::deckInfo(void) const{cout << "-----------------------------------\n";cout << "\tDeck Info:\n";cout << "\tNumber of Decks: " << numOfDecks << "\n";cout << "-----------------------------------\n";for(vector<Card>::const_iterator index = aDeck.begin(); index != aDeck.end(); ++index){index->cardInfo();}}shuffleDeck returns the following error:C:\Documents and Settings\Gokhan Husnu\My Documents\gamedev tuts\devwork\cards\Deck.cpp(53) : error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'const class Card' (or there is no acceptable conversion)C:\Documents and Settings\Gokhan Husnu\My Documents\gamedev tuts\devwork\cards\Deck.cpp(54) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Card' (or there is no acceptable conversion)for the last 2 lines of code in the for loop. How can I swap the values in the vector around?   
  You have made shuffleDeck const, which means it is not allowed to modify any of its member variables.  Since shuffleDeck is supposed to modify one of the member variables (the deck) it needs to be a non-const function.  You're also trying to assign an iterator to a Card and vice-versa.  You must dereference the iterator to get the card that it refers to.  Once you fix that you will discover that you are trying to assign through a const_iterator.  Again, const means the iterator is not allowed to be used to change the value of the Card it refers to so you must make it a non-const iterator.  With these fixes your code should look like:// remove both const qualifiers.  The first is redundant because the// function is returning nothing.  To distinguish between a nothing that can be// modified and one that can't is just silly.  The second was declaring that the// function would not modify member variables, which was wrong.void Deck::shuffleDeck(void){vector<Card>::size_type swapIndex;// we need a non-const iteratorvector<Card>::iterator index;for(index = aDeck.begin(); index != aDeck.end(); ++index){swapIndex = rand() % 52;Card temp = aDeck[swapIndex];// must dereference iterator to get card it refers toaDeck[swapIndex] = *index;// must dereference iterator to get card it refers to*index = temp;}cout << "-----------------------------------\n";cout << "\tDeck Shuffled\n";cout << "-----------------------------------\n";}This could be improved by making use of the random_shuffle algorithm from header <algorithm>:// Here I also removed the void in the parameter list, which is again// redundant in C++.  An empty parameter list in C++ means strictly no// parametersvoid Deck::shuffleDeck(){random_shuffle(aDeck.begin(), aDeck.end());cout << "-----------------------------------\n";cout << "\tDeck Shuffled\n";cout << "-----------------------------------\n";}Enigma   ;  When you say index = temp you are in fact trying to assign a Card to a vector<Card>::const_iterator if I'm not mistaken. You are also changing the aDeck vector while iterating through it, which will result in invalid iterators. Since you also are trying to change the index iterator the loop may never end, and I guess that is not you're intention [smile]. I think the easiest way to do what you want is to use random_shuffle:random_shuffle(aDeck.begin(), aDeck.end());Also, don't forget to include the algorithm-header if you use random_shuffle.(Doh! Beaten by 5 minutes? And I though I wrote fast :) )   ;  Here is what's returned when a new deck is created:-----------------------------------   Deck Info:   Number of Decks: 1-----------------------------------The Ace of HeartsThe Ace of SpadesThe Ace of ClubsThe Ace of DiamondsThe 2 of HeartsThe 2 of SpadesThe 2 of ClubsThe 2 of DiamondsThe 3 of HeartsThe 3 of SpadesThe 3 of ClubsThe 3 of DiamondsThe 4 of HeartsThe 4 of SpadesThe 4 of ClubsThe 4 of DiamondsThe 5 of HeartsThe 5 of SpadesThe 5 of ClubsThe 5 of DiamondsThe 6 of HeartsThe 6 of SpadesThe 6 of ClubsThe 6 of DiamondsThe 7 of HeartsThe 7 of SpadesThe 7 of ClubsThe 7 of DiamondsThe 8 of HeartsThe 8 of SpadesThe 8 of ClubsThe 8 of DiamondsThe 9 of HeartsThe 9 of SpadesThe 9 of ClubsThe 9 of DiamondsThe 10 of HeartsThe 10 of SpadesThe 10 of ClubsThe 10 of DiamondsThe Jack of HeartsThe Jack of SpadesThe Jack of ClubsThe Jack of DiamondsThe Queen of HeartsThe Queen of SpadesThe Queen of ClubsThe Queen of DiamondsThe King of HeartsThe King of SpadesThe King of ClubsThe King of DiamondsFirst I tried your suggestion Enigma, this is what I get:-----------------------------------   Deck Shuffled----------------------------------------------------------------------   Deck Info:   Number of Decks: 1-----------------------------------The 5 of HeartsThe Ace of DiamondsThe 2 of DiamondsThe 6 of ClubsThe -858993460 of -858993460The -858993460 of -858993460The 9 of HeartsThe -858993460 of -858993460The 7 of ClubsThe 4 of SpadesThe 8 of HeartsThe -858993460 of -858993460The -858993460 of -858993460The 10 of DiamondsThe 8 of ClubsThe -858993460 of -858993460The Jack of ClubsThe -858993460 of -858993460The 9 of SpadesThe King of SpadesThe -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The 10 of HeartsThe -858993460 of -858993460The 8 of SpadesThe -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The 6 of SpadesThe -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The Ace of SpadesThe -858993460 of -858993460The Queen of DiamondsThe -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The Ace of HeartsThe -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460I am guessing elements are being erased.Next I tried the random_shuffle:-----------------------------------   Deck Shuffled----------------------------------------------------------------------   Deck Info:   Number of Decks: 1-----------------------------------The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The Ace of HeartsThe -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460The -858993460 of -858993460Any ideas?   ;  I would guess the first place to look is the copy constructor and the operator= of the Card class. Seems like they don't do a copy.   ;  You shuffle algorithm is biased. There are 52^52 possible ways your algorithm could shuffle a deck, yet there are only 52! permutations of a deck. And 52! doesn't evenly divide into 52^52 (3 is a factor of 52!, but not 52^52), so it must be biased. If you swap your i'th element with a [i,n] element (inclusive) instead of [1,n], then your algorithm will no longer be biased (your number gen still could be, and likely is). The easy solution is to use random_shuffle as has already been suggested (the number gen can still bias this).   ;  I agree with the AP: the problem probably lies in your copy constructor and operator=(). Correct them, and it will probably be better.this gdnet thread presents a hat container that can be used to shuffle a deck (the hat can also do a lot of other operations). There is also an article about randomness that you may find usefull. HTH,   ;Quote:Original post by GokhanDeck::Deck(const Deck& deck){// copy constructor}!!If you want the default copying behaviour, then don't write a copy constructor at all. Writing this means that copy construction will not actually do any copying, or even any initializing of the Deck being constructed. If there is similar code in your Card class then that certainly explains the problem.P.S. As noted, the algorithm you're trying to use is not quite right (see here); std::random_shuffle() will get it correct. Also, for other tasks involving swapping, you don't have to write swaps yourself any more in C++; you can use std::swap:// old//Card temp = aDeck[swapIndex];//aDeck[swapIndex] = *index;//*index = temp;// newstd::swap(aDeck[swapIndex], *index);;  I am very silly, aren't I :| I don't know why I started writing copy constructors for both Card and Deck, and left them blank. I took out the offending code, and used random_shuffle, and now it works great! Thanks a lot everybody :)   ;  I am very interested in shuffling code, as I need some in order to get my idea for a type of card game sorta thing-o off the ground. Here is some simple code that can shuffle a deck nicely. I am only about 5 days into programming so it may not help but you are welcome to have a look at it.Shuffle_deck_v0.1.cpp Shuffle_deck_v0.1.exe There needs to be some addition fixes to the code.. for example the shuffle needs to check if the two random numbers it uses are the same before continuing as well as checking before the loop whether it has already used those two random numbers in a previous loop. I'll have a play with it latter and see if I can get it working like that.I have also done some research into shuffling... most of this is way beyond me and prob not relevant but have a look if you like.I looked around at various websites witch use randomness and have large users that pay cash. Online Casinos, Poker Sites, Magic the Gathering online etc are some of the ones I checked. They all seam to use algorizums from this guy called Knuth's and his book "Art of Computer Programming" Witch contains some famous (aparently) code for randomness and shuffeling. Like I said almost all online casinos, online CCGs and stuff seam to use them, as well as some partical simulation software I have looked at. Most of the infomation I have been gathering I can not understand yet as I am ust starting but I am looking into it.This is a openBSD source code for generating random seeds by gathering infomation from the local computers "noise".. read the notes in the code for info.... do not ask me to explain this as I am just collecting the data for latter like I said. Card shuffeling and randomness interests me a great deal.Random Seed Generator and Shuffle (openBSD)Here is a artical about the Magic The Gathering Online shuffler