Errors Using Random Numbers in a Class

closed account (Nw0DjE8b)
Hi, I've been working on a simple version of Mastermind using Microsoft Visual C++ 2010 Express. I use a random number generator to create the codemaker's code. However, I keep encountering the same problem:

1
2
3
4
5
6
7
8
  1
1>  mastermind.cpp
1>c:\users\matthew\documents\visual studio 2010\projects\mastermind\mastermind\code.h(9): error C2059: syntax error : 'constant'
1>  code.cpp
1>c:\users\matthew\documents\visual studio 2010\projects\mastermind\mastermind\code.h(9): error C2059: syntax error : 'constant'
1>c:\users\matthew\documents\visual studio 2010\projects\mastermind\mastermind\code.cpp(5): error C2660: 'code::distribution_one' : function does not take 1 arguments
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
  #include <random>
#include <string>
#include <iostream>

class code {
private:
	char maker_code[4];
	std::random_device generator_one;
	std::tr1::uniform_int_distribution<char> distribution_one(0, 5); /*
																			* Generates a random integer between 0 and 5
																			* Each number cooresponds to a color and letter.
																			* 0:		Red:		R
																			* 1:		Orange:		O
																			* 2:		Yellow:		Y
																			* 3:		Green:		G
																			* 4:		Blue:		B
																			* 5:		Violet:		V
																			*/
	char guess[4];
public:
	code();
	std::string make_code_debug();
	void input();
	char feedback[2];
	void provide_feedback();
	std::string ftostr(); 
};


1
2
3
4
5
6
7
8
9
  #include "code.h"

code::code() {
	for(char element = 0; element < 4; element++) // Assigns each element of the code a color
		maker_code[element] = distribution_one(generator_one);

	for(char element = 0; element < 2; element++)
		feedback[element] = 0;
}


I looked up both of the errors, but I can't figure out why I'm getting them. The documentation for C2059 is very obscure, and I'm properly passing arguments to the "distribution_one" function. I'll also add that this all worked before I integrated it into the class, so am I not defining things in the correct place?

Thanks.
Last edited on
Typically you use random_device to seed a generator. You don't use it as your generator.

Line 9 in your header file should be:

std::tr1::uniform_int_distribution<char> distribution_one ;

And the constructor definition should be modified:

code::code() : distribution_one(0,5) { // ...

closed account (Nw0DjE8b)
Thanks, it worked! I also think I understand why I was getting C2059 - "distribution_one" is a constant and I needed to initialize it with the constructor. And thanks for your insight on "random_device."
Topic archived. No new replies allowed.