Error C2883

I am trying to write a yahtzee program and I found a thread on here with a dice roller. I tried to compile it to see how it worked and it is giving me the following:

Line 9 Column 1
Error C2883: 'rand' : function declaration conflicts with source.cpp 'rand' introduced by using-declaration.

I checked and it doesn't seem like rand is being declared more than once so I have no idea what is going wrong with this.

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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int main () {

	int rand(void);
	int dice[5];
	int turn;
	char reroll[5];
	dice[0]=rand() % 6+1;
	dice[1]=rand() % 6+1;
	dice[2]=rand() % 6+1;
	dice[3]=rand() % 6+1;
	dice[4]=rand() % 6+1;
	turn=1;



	while (turn<=2) {
		cout << "Turn #" << turn;
		cout << "Your first roll is: " << dice[0] << dice[1] << dice[2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";
		cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
	if (reroll[0]=='y'){
		dice[0]=rand() % 6+1;}
	if(reroll[0]=='n'){
		dice[0];}
	if (reroll[1]=='y'){
		dice[1]=rand() % 6+1;}
	if (reroll[2]=='y'){
		dice[2]=rand() % 6+1;}
	if (reroll[3]=='y'){
		dice[3]=rand() % 6+1;}
	if (reroll[4]=='y'){
		dice[4]=rand() % 6+1;}
	cout << "Your second Roll is " << dice [0] << dice[1] << dice [2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";

	dice[0]=rand() % 6+1;
	dice[1]=rand() % 6+1;
	dice[2]=rand() % 6+1;
	dice[3]=rand() % 6+1;
	dice[4]=rand() % 6+1;
	turn++;}

	return 0;
}
It complains about line 9.
In addition, use <cstdlib> instead of <stdlib.h>
http://www.cplusplus.com/reference/cstdlib/rand/
So because I used <stdlib.h> rand is being declared twice?
When I changed them out I still get the same error.
No. It complains about line 9. rand function is already declared in cstdlib. So what exactly do you want to do with it on line 9? The compiler tries to create an int variable with the same name as a function already declared, and initialize it with a void. Obviously that does not work out well.
The compiler tries to create an int variable with the same name as a function already declared, and initialize it with a void. Obviously that does not work out well.


It is actually a prototype of a function called rand that returns an int and takes no arguments. That is why it is causing an error.
So that entire line doesn't need to be there?
Topic archived. No new replies allowed.