cross initialization error?

I'm getting this weird error that says "cross initialization of '__'" in this part of my code.
Do I need to get rid of the initializations, and I tried that, but I just get more weird errors.
Where can I initialize these variables without it being a problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  void Sketch::UpdateObject(int _x, int _y)
{
	switch(objects[n-1].tooltype)
	{
		case 0:
		case 1:
		case 2:
		case 3:
                        //x1, y1, x2, y2, and r won't compile.
			int x1=objects[n-1].tool.circle.center.x;
			int y1=objects[n-1].tool.circle.center.y;
			int x2=_x;
			int y2=_y;
			int r=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
			objects[n-1].tool.circle.radius=r;
			cout<<"Circle is updated"<<endl;
			break;
		case 4:
			objects[n-1].tool.endpoints.pt2.x=_x;
			objects[n-1].tool.endpoints.pt2.y=_y;
			cout<<"Line/Rect is updated"<<endl;
			break;			
	}
}
The simplest way is to limit the scope of those variables to only the block where they are used. Enclose the code for case 3 between opening/closing braces
{      }.

http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
I'm getting a full page of very weird errors I haven't seen before when I do that...
It's a bunch of paths and then "relocation 7 has invalid index 13" and that kind of thing...
Chervil's solution should fix the cross-initialisation issue, so it could be that the new errors are related to code elsewhere or the way your compiling (a cursory search brought up some people having g++ compilation issues).

If you post some error output then we might be able to narrow it down a little.
I can't compile that code as I don't have the rest of it. but what i had in mind was like this:
1
2
3
4
5
6
7
8
9
10
// Error
        case 3:                       
            int x1 = 1;

            cout << "Circle is updated" << x1 << endl;
            break;
        
        case 4:
            cout << "Line/Rect is updated" << endl;
            break;


1
2
3
4
5
6
7
8
9
10
11
12
// OK
        case 3:
        {                       
            int x1 = 1;

            cout << "Circle is updated" << x1 << endl;
            break;
        }
        
        case 4:
            cout << "Line/Rect is updated" << endl;
            break;
Hmm, for some reason, when I try using bash | tee file.txt in my terminal it saves the output as blank.
well, I changed a little.
Now there is only an issue with 'r'. Containing the rest of them like I did here worked, but it wouldn't work for 'r'.

Does anyone know any linux commands I can use to save my output as a text file besides bash | tee file.txt? haha

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
void Sketch::UpdateObject(int _x, int _y)
{
	switch(objects[n-1].tooltype)
	{
		case 0:
		case 1:
		case 2:
		case 3:
			int x1;
			{x1 = objects[n-1].tool.circle.center.x;}
			int y1;
			{y1 = objects[n-1].tool.circle.center.y;}
			int x2;
			{x2 = _x;}
			int y2;
			{y2 = _y;}
			int r = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
			objects[n-1].tool.circle.radius=r;
			cout<<"Circle is updated"<<endl;
			break;
		
		case 4:
			objects[n-1].tool.endpoints.pt2.x=_x;
			objects[n-1].tool.endpoints.pt2.y=_y;
			cout<<"Line/Rect is updated"<<endl;
			break;			
	}
}
Here is the program in its entirety, if this helps. It's in two files, they must be compiled together.

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "sketch.h"

#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

#define PI 3.1415926536

#define WIDTH    	700
#define HEIGHT   	600

#define MENUWIDTH 	100
#define BOXHEIGHT	(HEIGHT/NCOLORS)

#define DSLEFT    	MENUWIDTH
#define DSRIGHT  	WIDTH
#define DSBOTTOM  	0
#define DSTOP     	HEIGHT

#define NCOLORS 	8

#define RGBRED 		1, 0, 0
#define	RGBGREEN	0, 1, 0
#define RGBBLUE		0, 0, 1
#define RGBYELLOW	1, 1, 0
#define RGBMAGENTA	1, 0, 1
#define RGBCYAN		0, 1, 1
#define RGBBLACK	0, 0, 0
#define RGBWHITE	1, 1, 1

#define R		0
#define G		1
#define B		2

static float colormenu[8][3] = {{RGBRED}, {RGBGREEN}, {RGBBLUE}, {RGBYELLOW}, {RGBMAGENTA}, {RGBCYAN}, {RGBWHITE}, {RGBBLACK}};

//Line
void Sketch::CreateObject(int c, int t, int x1, int y1)
{
	objects[n].colortype=c;
	objects[n].tooltype=t;
	objects[n].tool.endpoints.pt1.x=x1;
	objects[n].tool.endpoints.pt1.y=y1;
	objects[n].tool.endpoints.pt2.x=x1;
	objects[n].tool.endpoints.pt2.y=y1;
	n++;
}

//Rectangle
void Sketch::CreateObject(int c, int t, int x1, int y1, int x2, int y2)
{
	objects[n].colortype=c;
	objects[n].tooltype=t;
	objects[n].tool.endpoints.pt1.x=x1;
	objects[n].tool.endpoints.pt1.y=y1;
	objects[n].tool.endpoints.pt2.x=x2;
	objects[n].tool.endpoints.pt2.y=y2;
	n++;
}

//Circle
void Sketch::CreateObject(int c, int t, int r, int x1, int y1)
{
	objects[n].colortype=c;
	objects[n].tooltype=t;
	objects[n].tool.circle.radius=r;
	objects[n].tool.circle.center.x=x1;
	objects[n].tool.circle.center.y=y1;
	n++;
}

void Sketch::UpdateObject(int _x, int _y)
{
	switch(objects[n-1].tooltype)
	{
		case 0:
		case 1:
		case 2:
		case 3:
			int x1;
			{x1 = objects[n-1].tool.circle.center.x;}
			int y1;
			{y1 = objects[n-1].tool.circle.center.y;}
			int x2;
			{x2 = _x;}
			int y2;
			{y2 = _y;}
			int r = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
			objects[n-1].tool.circle.radius=r;
			cout<<"Circle is updated"<<endl;
			break;
		
		case 4:
			objects[n-1].tool.endpoints.pt2.x=_x;
			objects[n-1].tool.endpoints.pt2.y=_y;
			cout<<"Line/Rect is updated"<<endl;
			break;			
	}
}

void Sketch::Load()
{
	string filename;
	ifstream fin;
	cout<<"Load: ";
	cin>>filename;
	fin.open(filename.c_str());
	if (fin.is_open())
	{		
		while (fin.good())
		{
			fin>>n;
			for(int i=0; i<n; i++)
			{
				fin>>objects[i].colortype;
				fin>>objects[i].tooltype;
				if(objects[i].tooltype==2 || objects[i].tooltype==3)
				{
				  fin>>objects[i].tool.circle.center.x;
				  fin>>objects[i].tool.circle.center.y;
				  fin>>objects[i].tool.circle.radius;
				}
				else
				{
				  fin>>objects[i].tool.endpoints.pt1.x;
				  fin>>objects[i].tool.endpoints.pt1.y;
				  fin>>objects[i].tool.endpoints.pt2.x;
				  fin>>objects[i].tool.endpoints.pt2.y;
				}
			}
		}
	}
	else{cout<<"Load failed";}
}

void Sketch::Save()
{
	string filename;
	ofstream fout;
	cout<<"Save as: ";
	cin>>filename;
	fout.open(filename.c_str());
	if(fout.is_open())
	{
		fout<<n<<" ";
		for(int i=0; i<n; i++)
		{
			fout<<objects[i].colortype<<" "<<objects[i].tooltype<<" ";
			if(objects[i].tooltype==2 || objects[i].tooltype==3)
			{
				fout<<objects[i].tool.circle.center.x<<" ";
				fout<<objects[i].tool.circle.center.y<<" ";
				fout<<objects[i].tool.circle.radius<<" ";
			}
			else
			{
				fout<<objects[i].tool.endpoints.pt1.x<<" ";
				fout<<objects[i].tool.endpoints.pt1.y<<" ";
				fout<<objects[i].tool.endpoints.pt2.x<<" ";
				fout<<objects[i].tool.endpoints.pt2.y<<" ";
			}
		fout<<endl;
		}
	}
	
}

void Sketch::Clear()
{
	n=0; 
	glutPostRedisplay;
}

void Sketch::drawRectangle(int index, GLint type)
{
	int c = objects[index].colortype;
	int x1 = objects[index].tool.endpoints.pt1.x;
	int y1 = objects[index].tool.endpoints.pt1.y;
	int x2 = objects[index].tool.endpoints.pt2.x;
	int y2 = objects[index].tool.endpoints.pt2.y;

	glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);

	glBegin(type);
		glVertex2i(x1,y1);
		glVertex2i(x1,y2);
		glVertex2i(x2,y2);
		glVertex2i(x2,y1);
	glEnd();
}


void Circle(GLint type)
{
	int i=36;                 
  	float angle;
  	float anglecounter= 2*PI/i; 
        
   	glBegin(type);
    	for(int k=0; k<i; k++)
	{
      		angle = k*anglecounter;
      		glVertex2f(cos(angle),sin(angle));
    	}
  	glEnd(); 
}

void Sketch::drawCircle(int index, GLint type)
{
	int c=objects[index].colortype;
	int x=objects[index].tool.circle.center.x;
	int y=objects[index].tool.circle.center.y;
	float r=objects[index].tool.circle.radius;

	glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);

	glPushMatrix();
		glTranslatef(x,y,0);
		glScalef(r,r,1);
		Circle(type);
	glPopMatrix();
}

void Sketch::drawLine(int index)
{
	int c=objects[index].colortype;
	int x1=objects[index].tool.endpoints.pt1.x;
	int y1=objects[index].tool.endpoints.pt1.y;
	int x2=objects[index].tool.endpoints.pt2.x;
	int y2=objects[index].tool.endpoints.pt2.y;

	glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);

	glBegin(GL_LINE_LOOP);
		glVertex2i(x1,y1);
		glVertex2i(x2,y2);
	glEnd();	
}

void Sketch::draw()
{
	for(int i=0; i<n; i++)
	{
		switch(objects[i].tooltype)
		{
			case 0: drawRectangle(i, GL_LINE_LOOP);
				break;

			case 1: drawRectangle(i, GL_POLYGON);
				break;

			case 2: drawCircle(i, GL_LINE_LOOP);
				break;

			case 3: drawCircle(i, GL_POLYGON);
				break;

			case 4: drawLine(i);
				break;

			case 5: drawLine(i);
				break;
		}
	}
	glutPostRedisplay();
}




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
#  include <GL/gl.h>
#  include <GL/glu.h>
#  include <GL/glut.h>

struct Point
{
	int x,y;
};

struct EndPoint
{
	Point pt1, pt2;
};
	
struct Circle
{
	Point center;
	int radius;
};

union Tool
{
	EndPoint endpoints;
	Circle circle;
};

struct Object
{
	short int colortype;
	short int tooltype;
	Tool tool;
};

class Sketch
{
	Object objects[10000];
	static int n;

		public:
		void CreateObject(int, int, int, int);
		void CreateObject(int, int, int, int, int);
		void CreateObject(int, int, int, int, int, int);
		void UpdateObject(int, int);
		void Save();
		void Load();
		void Clear();
		void drawRectangle(int, GLint);
		void drawCircle(int, GLint);
		void drawLine(int);
		void draw();	
};

int Sketch::n=0;


This is the second part of a three part code. It shouldn't really work or do anything, but it should be able to compile.
Last edited on
Well, I can see you haven't fixed the issue that Chervil described to you. You will need to include any variable you declare within a case statement inside of a block where it is used. The simplest way to do what Chervil posted and create a block for the entire case section.

The reason there is a problem is because of how a switch-case works. Look at your code:
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
switch(objects[n-1].tooltype)
{
	case 0:
	case 1:
	case 2:
	case 3:
		int x1;
		{x1 = objects[n-1].tool.circle.center.x;}
		int y1;
		{y1 = objects[n-1].tool.circle.center.y;}
		int x2;
		{x2 = _x;}
		int y2;
		{y2 = _y;}
		int r = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
		objects[n-1].tool.circle.radius=r;
		cout<<"Circle is updated"<<endl;
		break;
	
	case 4:
		objects[n-1].tool.endpoints.pt2.x=_x;
		objects[n-1].tool.endpoints.pt2.y=_y;
		cout<<"Line/Rect is updated"<<endl;
		break;			
}

Local variables, such as x1 in your example, are destructed when they leave scope. In your case, x1 is destructed on line 25, the closing brace of the switch block. However, if you go directly to case 4 then you don't initialize or construct x1 (because you jumped down to case 4, skipping that code), but then you proceed to try to destruct it on line 25. Hence the error you are getting.
Hmm. okay. I'm super new to this so I'm having quite a bit of trouble understanding some of this. I'm not sure what exactly that would look like in the code. Any other thing I do besides what I posted gives me really foreign looking errors.
This compiles for me:
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
void Sketch::UpdateObject(int _x, int _y)
{
    switch(objects[n-1].tooltype)
    {
        case 0:
        case 1:
        case 2:
        case 3:
        {
            int x1;
            x1 = objects[n-1].tool.circle.center.x;
            int y1;
            y1 = objects[n-1].tool.circle.center.y;
            int x2;
            x2 = _x;
            int y2;
            y2 = _y;
            int r = sqrt(pow(x2-x1,2)+pow(y2-y1,2));
            objects[n-1].tool.circle.radius=r;
            cout<<"Circle is updated"<<endl;
            break;
        }
        
        case 4:
            objects[n-1].tool.endpoints.pt2.x=_x;
            objects[n-1].tool.endpoints.pt2.y=_y;
            cout<<"Line/Rect is updated"<<endl;
            break;            
    }
}



Another error:
1
2
3
4
5
6
7
void Sketch::Clear()
{
    n=0; 
//    glutPostRedisplay; // error
    glutPostRedisplay();
    
}
Yes, I had tried that, it does not work for me for some reason. And thanks for catching that error.

The errors i get look like this:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/rt1.o(.debug_info):relocation 20 has invalid symbol index 19

it shows up with about 20 or so of those. What does that mean?
I would try doing a complete rebuild of your project, as that isn't an error with your code.
Topic archived. No new replies allowed.