expecting a constant expresion

In my main program I define a variable phantom_res:

const int phantom_res = 600;

Later in my main program I call on a function defined in the following h file:

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 "stdafx.h"
using namespace std;
#include "dm_Steps.h"
#include "dm_Tracks.h"
#include "dm_Data.h"

void print_data(vector<vector<vector<float>>> phantom_energy, int phantom_res, int chamber_radius, int width, int height, int depth)
{
	vector<float> bucket;
	float chamber_reading[phantom_res] = {0.0};
	float bucket_energy;
	int chamber_pos_start = width/2 - chamber_radius;
	int chamber_pos_end = width/2 + chamber_radius;

	ofstream myfile_1("C:file.txt");

	for (int k = chamber_pos_start; k < chamber_pos_end; k++)
	{
		for (int j = chamber_pos_start; j < chamber_pos_end; j++)
		{
			for (int i = 0; i < depth; i++)
			{
				bucket.push_back(phantom_energy[i][j][k]); 
			}

			for(int i = 0; i < bucket.size(); i++)
			{
				chamber_reading[i] += bucket.at(i); 
			}
			bucket.clear();
		}
	}

	for(int i = 0; i < depth; i++)
		myfile_1 << chamber_reading[i] << endl;
}


When I try to compile the program I get the following error on line 10 of my h file:

Error 15 error C2057: expected constant expression

I believe this happens because the compiler is not reading phanton_res in my h file as a constant. How do I fix this problem?
The compiler is seeing phantom_res as a non-constant value because your parameter is shadowing the pre-defined constant.

Read my last post in this thread: http://www.cplusplus.com/forum/general/60417/#msg327215

Wazzak
Last edited on
I believe this happens because the compiler is not reading phanton_res in my h file as a constant.

That's not the cause. The compiler is complaining because your print_data function is taking in a phantom_res as a parameter. This phantom_res is hiding the phantom_res in your header.
Ok I understand that my function is taking phantom_res as a parameter. But how do I make the function take it as a constant. Would I need to define phantom_res as 'const int phantom_res' within my function cause that didn't seem to work either.
Don't make your function take it as a parameter...at all
How I define it in my function without making it a parameter? Make it global?
Ah, I misunderstood when you said this:
I believe this happens because the compiler is not reading phanton_res in my h file as a constant.
I thought it already was global.

For constants such as this, I think it is a little bit slightly ok to an extent to maybe declare it possibly as a global variable perhaps. Though having any globals at all is a sign of a design flaw...
ok I declared the phantom_res as global and I am not getting any errors. However when I run my program, the print_data function never finishes executing and keeps running. Here is my main program:
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "stdafx.h"
//using namespace std;
#include "dm_Steps.h"
#include "dm_Tracks.h"
#include "dm_Data.h"
#include "binary_search.h"
#include "print_data.h"
#include "gaussian_generator.h"
#include "print_data_2D.h"
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_01.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random.hpp> 



int main() 
{
	//User selects proton energy
	std::cout << "Enter Proton Energy (MeV): ";
	float key;
	std::cin >> key;
	
	//User selects proton flux
	std::cout << "Enter Proton Flux (x1000): ";
	int k_proton_flux;
	std::cin >> k_proton_flux;

	double t0 = clock();
	std::cout << endl;

	ifstream myfile("C:file.txt");    

	// Check if file is found. 
	if (!myfile) 
	{
		std::cout << "Unable to open file!!! " << endl;
		std::cin.get();
		exit(1); 
	}

	//Create new data base of proton tracks of selected energies
	////////////////////////////////////////////////////////////////////////////////////////
	dm_Data data_base_1 = binary_search(myfile, key);

	//Intialize water phantom
	////////////////////////////////////////////////////////////////////////////////////////
	const int phantom_size = 25; //mm
	//const int phantom_res = 600;
	float voxel_size = float(phantom_size)/float(phantom_res);

	const int chamber_radius = 2.5*(phantom_res/phantom_size); 
	const int depth = phantom_res;  //x
	const int height = phantom_res; //y
	const int width = phantom_res;  //z

	std::cout << "Voxel Size = " << voxel_size << " mm^3" << endl;
	std::cout << "Creating 25x25x25mm Water Phantom..." << endl;
	vector<vector<vector<float>>> phantom_energy;

	phantom_energy.resize(height);
	for (int i = 0; i < height; ++i) 
	{
		phantom_energy[i].resize(width);
		for (int j = 0; j < width; ++j)
			phantom_energy[i][j].resize(depth);
	}

	for( int i = 0; i < height; i++)
	{
		for( int j = 0; j < height; j++)
		{
			for( int k = 0; k < height; k++)
			{
				phantom_energy[i][j][k] = 0;
			}
		}
	}

	//Randomize the entry point of the proton tracks (contained in data_base_2)
	////////////////////////////////////////////////////////////////////////////////////////
	dm_Data data_base_2 = gaussian_generator(data_base_1, k_proton_flux, phantom_size);

	std::cout << "Exposing Phantom..." << endl;

	std::cout << " " << endl;	

	//vector<vector<vector<float>>> phantom_energy = expose_phantom(phantom_res, phantom_size, width, height, depth, phantom_energy, data_base_2);
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	boost::minstd_rand intgen;
	boost::uniform_01<boost::minstd_rand> gen(intgen);

	for(int track_num = 0; track_num < data_base_2.m_data.size(); track_num++)
	{
		for(int step_num = 0; step_num < data_base_2.m_data.at(track_num).m_track.size() - 2; step_num++)
		{
			float energy = (data_base_2.m_data.at(track_num).m_track.at(step_num).m_energy_loss);

			float x1 = data_base_2.m_data.at(track_num).m_track.at(step_num).m_xpos;
			float x2 = data_base_2.m_data.at(track_num).m_track.at(step_num + 1).m_xpos;
			float y1 = data_base_2.m_data.at(track_num).m_track.at(step_num).m_ypos;
			float y2 = data_base_2.m_data.at(track_num).m_track.at(step_num + 1).m_ypos;
			float z1 = data_base_2.m_data.at(track_num).m_track.at(step_num).m_zpos;
			float z2 = data_base_2.m_data.at(track_num).m_track.at(step_num + 1).m_zpos;
			float m = gen();
			float x = x1 + m*(x2 - x1);
			float y = y1 + m*(y2 - y1);
			float z = z1 + m*(z2 - z1);

			int energy_index_x = floor(x*(phantom_res/phantom_size));
			int energy_index_y = ceil(y*(phantom_res/phantom_size) + height/2);
			int energy_index_z = ceil(z*(phantom_res/phantom_size) + width/2);

			if(energy_index_x > phantom_res, energy_index_y > phantom_res, energy_index_z > phantom_res)
				continue;

			phantom_energy[energy_index_x][energy_index_y][energy_index_z] += energy;
		}
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	std::cout << "Measuring Central Axis Energy Deposition..." << endl;
	print_data(phantom_energy, chamber_radius, width, height, depth);
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/*std::cout << "Measuring Central Axis Energy Deposition..." << endl;
	vector<float> bucket;
	float chamber_reading[phantom_res] = {0};
	float bucket_energy;
	int chamber_pos_start = width/2 - chamber_radius;
	int chamber_pos_end = width/2 + chamber_radius;

	ofstream myfile_1("C:file.txt");

	for (int k = chamber_pos_start; k < chamber_pos_end; k++)
	{
		for (int j = chamber_pos_start; j < chamber_pos_end; j++)
		{
			for (int i = 0; i < depth; i++)
			{
				bucket.push_back(phantom_energy[i][j][k]); 
			}

			for(int i = 0; i < bucket.size(); i++)
			{
				chamber_reading[i] += bucket.at(i); 
			}
			bucket.clear();
		}
	}

	for(int i = 0; i < depth; i++)
		myfile_1 << chamber_reading[i] << endl;*/


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	//print_data_2D(phantom_energy, phantom_size, width, height, depth, phantom_res);
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	std::cout << "Creating 2D Energy Deposition Plot..." << endl;
	ofstream myfile_2("C:\\Users\\Dominic\\Documents\\MATLAB\\Track Repeating\\2D_DD.txt");

	for (int i = 0; i < phantom_res; i++)
	{
		for (int j = 0; j < phantom_res; j++)
		{
			myfile_2 << phantom_energy[i][j][height/2] << " ";
		}

		myfile_2 << " " << endl;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	std::cout << "Done (main_program_test_4_working_out_function_calls)" << endl;
	double t1 = clock();
	double elapsed = t1 - t0;
	std::cout << "Time Elapsed: " << elapsed/CLOCKS_PER_SEC << endl;
	std::cin.get();
	return 0;
}


You can see when I make the print_data function call on line 126. Here is my print_data h file which contains my function:

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
#include "stdafx.h"
using namespace std;
#include "dm_Steps.h"
#include "dm_Tracks.h"
#include "dm_Data.h"

void print_data(vector<vector<vector<float>>> phantom_energy, int chamber_radius, int width, int height, int depth)
{
	std::cout << "hey dom" << endl;
	vector<float> bucket;
	float chamber_reading[phantom_res] = {0.0};
	float bucket_energy;
	int chamber_pos_start = width/2 - chamber_radius;
	int chamber_pos_end = width/2 + chamber_radius;

	ofstream myfile_1("C:file.txt");

	for (int k = chamber_pos_start; k < chamber_pos_end; k++)
	{
		for (int j = chamber_pos_start; j < chamber_pos_end; j++)
		{
			for (int i = 0; i < depth; i++)
			{
				bucket.push_back(phantom_energy[i][j][k]); 
			}

			for(int i = 0; i < bucket.size(); i++)
			{
				chamber_reading[i] += bucket.at(i); 
			}
			bucket.clear();
		}
	}

	for(int i = 0; i < depth; i++)
		myfile_1 << chamber_reading[i] << endl;
}


Any ideas why my print_data function just keeps running. When I take the code out of the h file and put it straight into my main function, the code seems to work fine.
Topic archived. No new replies allowed.