How to use a class with a array of objects

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
class FlashCard
{
public:
    int a,b;
    void PrintCard(void);
    int CorrectAnswer(void);
};

void SwapFlashCard(FlashCard &a,FlashCard &b)
{
	FlashCard c;
	c=a;
	a=b;
	b=c;
}

void ShuffleCards(FlashCard &card)
{
	int i;
	for(i=0; i<144; i++)
	{
		card[i].a=1+i%12;
                card[i].b=1+i/12;
	}
    
	for(i=0; i<144; i++)
	{
		int r=rand()%144;
		SwapFlashCard(card[i].a,card[r].a);
                SwapFlashCard(card[i].b,card[r].b);
	}
}

int main(void)
{
FlashCard card[144];
...........
}


Why does Xode warn me that Type'FlashCard'does not provide a subscript operator on line 22,23,29 and 30?
Last edited on
Because your parameter for the ShuffleCards function references a single FlashCard object, and since it is not an array, you are not permitted to use the subscript operator. If you're trying to pass the whole array do something along these lines:
void ShuffleCards(FlashCard card[])
When I call the ShuffleCards function, how do I write my input?
When I change void ShuffleCards(FlashCard &card) into void ShuffleCards(FlashCard card[]),there are on warnings on line 22 and 23 anymore,but there are warnings on line 29 and 30 saying that no matching function for call to 'SwapFlashCard'.
Is my definition of SwapFlashCard wrong?
(1) You would call the function like so: ShuffleCards( array_name );

As far as errors on 29/30, you have to look at the types of variables you are passing.

SwapFlashCards takes two parameters both of which or FlashCard objects. On lines 29/30 you are not pass FlashCard objects, you are passing char[i].a, card[r].a, card[i].b, card[i].b, which are all ints defined inside the FlashCard object, but ints nonetheless. So your compiler is telling you that there is no SwapFlashCards(int, int) function.
So the right way to correct line is to combine line 29 and 30 into SwapFlashCard(card[i],card[r])? In this way,does the function will also swap each a and b member variables inside each card object?
That seems to be the case. Yes.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


class FlashCard
{
public:
    int a,b;
    void PrintCard(void);
    int CorrectAnswer(void);
};

/*void FlashCard::PrintCard()
{
    fgets(<#char *#>, <#int#>, <#FILE *#>);
    atoi(<#const char *#>);
}*/
int FlashCard::CorrectAnswer()
{
    return a*b;
}

template <class FlashCard>
void SwapFlashCard(FlashCard &a,FlashCard &b)
{
    FlashCard tmp = a;
    a = b;
    b= tmp;
}

void ShuffleCards(FlashCard card[])
{
	int i;
	for(i=0; i<144; i++)
	{
		int r=rand()%144;
		SwapFlashCard(card[i],card[r]);
	}
}

int main(void)
{
	FlashCard card[144];
    int nCard;
	unsigned int t0;
    
	for(;;)
	{
		printf("How many cards to work on?\n");
		printf(">");
		scanf("%d",&nCard);
        
		if(nCard<1 || 144<nCard)
		{
			printf("The number of cards must be between 1 and 144.\n");
		}
		else
		{
			break;
		}
	}
    
	t0=time(NULL);
	srand(t0);
    
	ShuffleCards(card);
    
	int nCorrect=0;
	for(int i=0; i<nCard; i++)
	{
		int answer;
		card[i].a=1+i%12;
		card[i].b=1+i/12;
		printf("%dx%d=",card[i].a,card[i].b);
		scanf("%d",&answer);
		if(card[i].a*card[i].b==card[i].CorrectAnswer())
		{
			printf("Correct!\n");
			nCorrect++;
		}
		else
		{
			printf("Wrong!  Correct answer is %d\n",card[i].CorrectAnswer());
		}
	}
    
	printf("You have worked on %d problems.\n",nCard);
	printf("You answered %d problems correctly. (%d%%)\n",nCorrect,nCorrect*100/nCard);
	printf("You spent %d seconds to answer all problems.\n",time(NULL)-t0);
    
	return 0;
}


It seems the shuffle function dosen't work.What's wrong with the ShuffleCards function?
Last edited on
Topic archived. No new replies allowed.