I have two questions, the second being optional.
First, in the program below (a prototype of a simple card program), I am getting the following error:
(29): error C2660: ‘shuffle’ : function does not take 1 arguments
with the following code:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <deque>
#include <algorithm>
using namespace std;
deque<int> cardDeck (51);
void flip(); //Prototype flip()
void shuffle(); //Prototype shuffle()
int _tmain(int argc, _TCHAR* argv[])
{
ostream& operator<<(ostream& os, deque<int> dq); //overload << operator to accept deque
//arguments
for (int a=52; a>0; a--) { //initialize the 52 cards in a deck
cardDeck.push_front(a);
}
flip(); //prompt my input to check data
return 0;
}
void flip() { //flip over card in specified location in the deck
int input;
cin >> input;
cout<<cardDeck[input]<<endl;
shuffle(cardDeck);
flip();
}
void shuffle(deque<int> dq) { //use Fisher-Yates algorithm to efficiently and accurately
//randomize card order
for(int i=dq.size()-1; i>-1; i--) {
int j = rand() % (i + 1);
if(i != j) {
swap(dq[j], dq[i]);
}
}
}
Why do I receive this error? (I have looked around and attempted to solve it myself)
Secondly, I’m not certain if I’m doing the fisher-yates algorithm properly because c++ documentation isn’t easy to find on it (for the version that utilizes swap();) (Brownie points for answering this or pointing out any horribly awful coding practices, not including the lack of classes)
Thanks in advance!
The reason you get that error is because you declare
shuffleas a function not taking any arguments.Another note is that you probably want to take a reference to the deque in that function, otherwise you’ll shuffle a local copy and won’t have the desired side effect.
You probably want it to lok like this:
Also, you might want to use
iter_swapinstead ofswapto swap the elements. In a dequeue it probably won’t make a difference, but forlistormapit would.