1

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!

Aarowaim
  • 781
  • 3
  • 10
  • Taking up your offer of pointing out horribly awful coding practices: 1) use `main` instead of `_tmain`, as `_tmain` needlessly limits the code's portability. 2) use `void shuffle(deque& dq)`, otherwise the program will make a copy of the deque and shuffle that. 3) Don't `using namespace std` at file scope. Prefer to fully qualify names, or do `using` within the scope of a function. 4) If you want to print deques, have a look at [this](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers). 5) `shuffle` is correct, but it can be implemented as `std::random_shuffle`. – Mankarse Sep 08 '11 at 05:10
  • Don't worry too much though. None of those points are actually particularly horrible. ;) – Mankarse Sep 08 '11 at 05:11
  • Thnx for the feedback. The reason I wasn't sure about the algorithm was because it was putting lots of 0s in the deck, but a call to 'deque.erase' removed the 0s that were in positions above cardDeck[52]. I use _tmain because it's a nuisance to change it, especially when I program as often as I do. I only just came to terms with dereferencing and since then, I have implemented it. As for 'using namespace' I merely use it for convenience, but on bigger programs that I might release or show friends, I use 'std::'. the 5th one is going to be very useful for me in the future. Thanks ^^ – Aarowaim Sep 23 '11 at 22:44

3 Answers3

2

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.

Arvid
  • 10,352
  • 1
  • 27
  • 35
  • Thank you very much, I was so busy being a programmer and looking for the most obscure cause of the problem that I forgot the obvious //*facepalm*. It's working fine now, and thank you as well, for the tip on passing a reference. – Aarowaim Sep 08 '11 at 04:52
0

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

void shuffle();

should be

void shuffle(deque<int> dq);
mwoz
  • 5
  • 2
0

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!

templatetypedef
  • 328,018
  • 92
  • 813
  • 992