Errors in header file

Hello everyone! I just started learning C++ and I've created a header file (generate_input.hpp) to use in two source programs (generate_input.cpp and generate_test.cpp). I get a bunch of errors and I know where the problem is, but I just can't figure it out on my own, so I hope there's someone that wants to help me. Thanks in advance!

Here's my code:

generate_input.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef GENERATE_INPUT_H_
#define GENERATE_INPUT_H_

#include <gmp.h>
#include <flint.h>
#include <fmpz.h>
#include <fmpz_poly.h>
#include <cstdlib>

extern const int length;

void rand_code(fmpz_t (&u_code)[length]);
void inv_code(fmpz_t u_code[], fmpz_t (&u_code_inv)[length]);
void rand_mask(fmpz_t (&u_mask)[length]);
void combined_masks(fmpz_t (&u_maskAB)[length], fmpz_t u_maskA[length], fmpz_t u_maskB[length]);
#endif //GENERATE_INPUT_H_ 


generate_input.cpp
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
#include <gmp.h>
#include <flint.h>
#include <fmpz.h>
#include <fmpz_poly.h>
#include <iostream>
#include <cstdlib>

#include "generate_input.hpp"	

void rand_code(fmpz_t (&u_code)[length]) {
	for(int i=0; i<length; i++) {
		fmpz_set_ui(u_code[i], rand()%2);
	}
}

void inv_code(fmpz_t u_code[], fmpz_t (&u_code_inv)[length]) {
	for(int i=0; i<length; i++) {
		fmpz_set_ui(u_code_inv[i], fmpz_is_zero(u_code[i]));
	}
}

//generate random mask
//bits in corresponding code that is erroneous are set to 0 in the mask, all other bits to 1
void rand_mask(fmpz_t (&u_mask)[length]) {
	int chance = 90; //chance in % that corresponding bit in code is valid

	for(int i=0; i<length; i++) {
		fmpz_set_ui(u_mask[i], (rand()%100)<chance);
	}
} 

void combined_masks(fmpz_t (&u_maskAB)[length], fmpz_t u_maskA[], fmpz_t u_maskB[]) {
	for(int i=0; i<length; i++) {
		fmpz_and(u_maskAB[i], u_maskA[i], u_maskB[i]);
	}
}


generate_test.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <gmp.h>
#include <flint.h>
#include <fmpz.h>
#include <fmpz_poly.h>
#include <iostream>
#include <cstdlib>

#include "generate_input.hpp"

using namespace std;

const int length;

int main() {
	length=20;

	//random generation of codeA
	fmpz_t u_codeA[length];
	for(int i=0; i<length; i++) {
		fmpz_init(u_codeA[i]);
	}

	rand_code(u_codeA);

	cout << "codeA : ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_codeA[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_codeA[i]);
	}

	//inverse of codeA
	fmpz_t u_codeA_inv[length];
	for(int i=0; i<length; i++) {
		fmpz_init(u_codeA_inv[i]);
	}

	inv_code(u_codeA, u_codeA_inv);
	
	cout << "inverse of codeA: ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_codeA_inv[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_codeA_inv[i]);
	}

	//random generation of codeB
	fmpz_t u_codeB[length];
	for(int i=0; i<length; i++) {
		fmpz_init(u_codeB[i]);
	}
	rand_code(u_codeB);
	cout << "codeB : ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_codeB[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_codeB[i]);
	}

	//inverse of codeB
	fmpz_t u_codeB_inv[length];
	for(int i=0; i<length; i++) {
		fmpz_init(u_codeB_inv[i]);
	}

	inv_code(u_codeB, u_codeB_inv);
	
	cout << "inverse of codeB: ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_codeB_inv[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_codeB_inv[i]);
	}

	//generation of maskA
	fmpz_t u_maskA[length];

	for(int i=0; i<length; i++) {
		fmpz_init(u_maskA[i]);
	}

	rand_mask(u_maskA);
	
	cout << "maskA: ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_maskA[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_maskA[i]);
	}

	//generation of maskB
	fmpz_t u_maskB[length];

	for(int i=0; i<length; i++) {
		fmpz_init(u_maskB[i]);
	}

	rand_mask(u_maskB);
	
	cout << "maskB: ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_maskB[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_maskB[i]);
	}

	//maskA ^ maskB
	fmpz_t u_maskAB[length];

	for(int i=0; i<length; i++) {
		fmpz_init(u_maskAB[i]);
	}

	combined_masks(u_maskAB, u_maskA, u_maskB);
	
	cout << "maskAB: ";
	for(int i=0; i<length; i++) {
		fmpz_print(u_maskAB[i]);
	}
	cout << endl;

	for(int i=0; i<length; i++) {
		fmpz_clear(u_maskAB[i]);
	}

	return 0;
}


I'm sorry for the very long codes. I guess there's no need to look at generate_test.cpp, except for the first 15 lines maybe.

I get the following errors:
1
2
3
4
5
6
7
8
generate_input.hpp:15:40: error: variable or field "rand_code" declared 
void void rand_code(fmpz_t (&u_code)[length]);
generate_input.hpp:15:25: error: "u_code" was not declared in this scope 
void rand_code(fmpz_t (&u_code)[length]);
generate_input.hpp:15:31: functional cast to array type "fmpz_t {aka long int[1]}" 
void rand_code(fmpz_t (&u_code)[length]);
generate_input.hpp:16:59: error: array integer bound is not an integer constant before "]" token 
void inv_code(fmpz_t u_code[], fmpz_t (&u_code_inv)[length]);


These errors repeat for the other functions in generate_input.hpp. I guess the problem is in the arguments (&some_variable)[length], i.e. that length has not been initialized in the header file, but I don't see a solution as I'm a beginner and have only little knowledge of C++ (I'm more experienced in Java). I hope someone can help me, thanks a lot!
change the prototypes so you send the size of the array
void rand_code(fmpz_t u_code[], size_t lenght);
Last edited on
Thanks a lot! It works. :)
Topic archived. No new replies allowed.