Question

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!

Was it helpful?

Solution

The reason you get that error is because you declare shuffle as a function not taking any arguments.

void shuffle();

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:

void shuffle(deque<int>& dq);

Also, you might want to use iter_swap instead of swap to swap the elements. In a dequeue it probably won't make a difference, but for list or map it would.

OTHER TIPS

I think you forgot to put the argument in your function declaration

void shuffle();

should be

void shuffle(deque<int> dq);

I think that the problem is that at the top of your program you've prototyped `shuffle as

void shuffle();

Notice that this takes no arguments. Because C++ uses a one-pass compiler, at the point that you call shuffle, this is the only declaration of shuffle available because the compiler hasn't seen the implementation later on. Consequently, it gives you the above error, because it thinks you are calling a zero-argument function with one argument.

To fix this, update the prototype so that it matches the function you've actually defined.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top