confusing compiler error

i am trying to write a program that will shuffle a deck of an unspecified number of cards without duplicating or losing any, then print their values out on the screen. i have code, but i am getting some confusing error messages. here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//shuffle class attempt
#include <iostream>
using namespace std;

class shuffle		//class declaration
{public:
    shuffle(int card_deck[], int number, int now);
    void mix();	
private:
    int deck[];
    int n;
	int current_card;	//declare and initialize current array member	
}
shuffle::shuffle(int card_deck[], int number, int now)    //initialize variables with constructor
{  deck[]=card_deck[];
   current_card=now;
   n=number;
}
shuffle::mix()
{while(current_card<=n)	//while there are cards left in the deck, continue the loop
	{int last_card_position=current_card-1;	//declare and initialize decreasing variable for check loop to prevent duplicate cards
		int b=rand_int(0,n);
		int check()		//loop for duplicate check
		{if(b=deck[last_card_position])	//if the random # is equal to any of the previous values, rerandomize and check again.
			{b=rand_int(0,n);
				check();}
		else if(last_card_position<=0)break;	//if the random # is unique so far, and the card just checked was in the 0 position, end loop
		else 				//if the random # is unique so far, and the card != 0, decrease the array position by one and check again
		{last_card_position--;
			check();}
		}
		deck[n]=b;				//if the random # is unique thus far, write it to its position in the array
		current_card++;           //move to next position in array and start over
   }
}
int main()
{cout<<"How many cards are in the deck?";
   cin>>int cards;
   int cards[]=for(int j=0;j=n;j++)cards[j]=j;
   shuffle shuffle(cards[], 0, cards);
   shuffle(cards[],cards);
   cout<<"The values of the cards are ";
   for(i=0;i=n;i++){cout<<deck[i];}
   return 0;
}


for some reason, in my constructor for my shuffle class, i am getting an error as follows: "new types may not be defined in a return type"
i won't list any more errors, as many likely refer to these initializations
Last edited on
You are a bit way off on this:

1. A class declaration should have a semicolon after the cloasing brace like this:
1
2
3
4
class classname
{

};
That would get rid of the error you mentioned.

2. In your class declaration you have:
1
2
private:
    int deck[];

You cannot declare an unspecified size array like this. You have to give it a size.

3. Line 15:
 
deck[]=card_deck[];

You cannot assign arrays like this.
You have to copy the data from one array to another (although someone may come along and correct me on this).

4. Line 23 - 30
 
int check()

You cannot have functions within functions (nested functions) in c++;

5. Line 19 - Shuffle::mix function
Because of problem 4 mentioned above and problems with { and } not matching this function is all wrong.

6. Lines 36 - 43 int main funtion
line 39 int cards[]=for(int j=0;j=n;j++)cards[j]=j
line 43 for(i=0;i=n;i++){cout<<deck[i];}
These for loops are almost certainly wrong because j=n and i=n are assignments not tests and would probably give infinite loops or the loops won't run at all.

7. Line 25 {b=rand_int(0,n);
Where does rand_int come from???



Last edited on
3)as in using a loop and copying them value by value? also, can i somehow use a pointer for that?
6)i thought they just needed to be variables. what letters are acceptable for those positions?
7)ok, i admit the c++ textbook i've been studying was published in 1997, and rand_int(min_value, max_value) was what they said to use as a randomizer....

i think i fixed 1-4 and maybe 5. as for 6 and 7, what is a good letter for a test and the syntax and code for the randomizer function?
here is what i have now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//shuffle class attempt
#include <iostream>
using namespace std;
int cards[104];
int number_of_cards;
class shuffle		//class declaration
{public:
    shuffle();
    int mix(int n, int current_card);	
private:
    int n;
	int current_card;	//declare and initialize current array member	
};
shuffle::shuffle()    //initialize variables with constructor
{  current_card=0;
   n=number_of_cards;
}
shuffle::mix(int n, int current_card)
{while(current_card<=n)	//while there are cards left in the deck, continue the loop
	{int last_card_position=current_card-1;	//declare and initialize decreasing variable for check loop to prevent duplicate cards
		int b=rand_int(0,n);
		while(last_card_position<=0)
        {   if(b=cards[last_card_position])	//if the random # is equal to any of the previous values, rerandomize and check again.
			{   b=rand_int(0,n);
			}
		    else 				//if the random # is unique so far, and the card != 0, decrease the array position by one and check again
		    {   last_card_position--;
		    }
		}
		cards[n]=b;				//if the random # is unique thus far, write it to its position in the array
		current_card++;           //move to next position in array and start over
   }
}
int main()
{cout<<"How many cards are in the deck?";
   cin>>number_of_cards;
   for(int j=0;j=number_of_cards;j++){cards[j]=j;}
   shuffle.mix(number_of_cards, 0);
   cout<<"The values of the cards are ";
   for(int i=0;i=number_of_cards;i++){cout<<cards[i];}
   return 0;
}

the error i am having trouble with is on line 19: "ISO C++ forbids declaration of 'mix' with no type"
Last edited on
The C++ standard says that functions must have an explicit return type. (And that the declaration must match the definition.)
 
int shuffle::mix(int n, int current_card)


As a suggestion, I think you would have a lot better help if you were a little more strict on how you used { and } and indentation. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int cards[ 104 ];
int number_of_cards;

class shuffle
{
    public:
        void shuffle();
        int mix( int n, int current_card );

    private:
        int n;
        int current_card;
};

void shuffle::shuffle()
{
    current_card = 0;
    n = number_of_cards;
}

...

Make sure that indentation matches. Further, even though C and C++ syntax "encourages" this, try to keep separate statements separate. Here is a cleaned-up version of your mix() function. Notice the liberal use of whitespace to give visual clarity to the structure. (Remember, you aren't just programming the computer, you are programming your brain.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int shuffle::mix( int n, int current_card )
{
    // While there are spaces left to fill in the deck... randomly generate cards
    while (current_card <= n)
    {
        // 'last_card_position' is used to count down through the deck so we can check against duplicate cards
        int last_card_position = current_card -1;

        // 'b' is the new random card number
        int b = rand_int( 0, n );

        // Count through the already generated cards in the deck and look to see if 'b' is already in there
        while (last_card_position <= 0)
        {
            if (b == cards[ last_card_position ])
            {
                // We found a duplicate. Get a new random card and start looking for duplicates all over again
                b = rand_int( 0, n );
                last_card_position = current_card -1;  // (don't forget this!)
            }
            else
            {
                last_card_position--;
            }

        // This doesn't belong here...
        cards[ n ] = b;
        current_card++;
        }

    // ...it belongs here. Remember to say what it does.
    }

// The function is declared to return 'int', but it has no 'return' statement (your compiler should complain).
}


Some of your commentary is very good, particularly on lines 19..30. Things that state something obvious or unclear are not good, like 6, 12, and 14. Line 31's comment is unclear, particularly as it is in the wrong loop.

I hope this visual reorganization helps. It really does make a huge difference in reading and understanding the code (while programming it). Enjoy!
Darth, consider using the random_shuffle algorithm which will accomplish in 1 line of code what you've done with the entire class.

1
2
3
4
5
6
#include <algorithm>  // For std::random_shuffle()

int deck[ 52 ]; // Assume this is your deck

// Shuffle the cards
std::random_shuffle( deck, deck + 52 ); // deck + 52 is correct, deck + 51 is not. 


Standard algorithms are nice when you don't have to learn how things work underneath.
jsmith, thank you! what are the arguements for the random_shuffle class (i.e. what do 'deck' and 'deck + 52' stand for)?
and if i could learn from the source of that algorithm, i would like it (i.e. where do i find the code supporting it?)
Last edited on
arguments:
http://www.cplusplus.com/reference/algorithm/random_shuffle.html

source:
Somewhere in your compiler's "include" path (on unix it is usually /usr/include, on windows it is usually C:\...\MyCompiler\include) there is a subdirectory for C++ stuff (quite often named "C++").

Dig down in there until you find a file named "algorithm". Open it up (use 'less' on unix or 'more' or 'notepad' on Windows) and take a look inside to see what files it includes. Find those files and grep (unix) or find (windows) for "random_shuffle".

Open the file that comes up and take a look inside.

Keep in mind, these are template functions, so if you aren't used to looking at it the reading is not entirely straight-forward.

Good luck.
Topic archived. No new replies allowed.