what is wrong with these codes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	switch(current_chunk_id%4)
	{
		case 0:
			PChunk chunk = new Chunk(current_chunk_id++, gCurrentTime, 0, 0.15);
			break;
		case 1:
			PChunk chunk = new Chunk(current_chunk_id++, gCurrentTime, 1, 0.075);
			break;
		case 2:
			PChunk chunk = new Chunk(current_chunk_id++, gCurrentTime, 2, 0.0375);
			break;
		case 3:
			PChunk chunk = new Chunk(current_chunk_id++, gCurrentTime, 3, 0.01875);
			break;
		default:
			 fatal("something wrong with new chunk generation\n");
	}


the error information is:
Description Resource Path Location Type
crosses initialization of ‘Chunk* chunk’ videostream.cpp p2ptv line 105 C/C++ Problem

Description Resource Path Location Type
‘chunk’ was not declared in this scope videostream.cpp p2ptv line 120 C/C++ Problem
Last edited on
You can't declare chunk in that way. Move the declaration out like:

1
2
3
4
5
6
PChunk chunk = 0;
switch (current_chunk_id % 4)
{
case 0:
    chunk = new Chunk(current_chunk_id++, gCurrentTime, 0, 0.15);
    break;

... and so on.
why? I'm confused about the scope of variables

it is a little strange that if we declare some variables within a pair of curly bracket { }
it will disappear outside the {}?

so if I define some variables within
if()
{
int i=3;
}

cout<<i<<; // is there any error?
it is a little strange that if we declare some variables within a pair of curly bracket { }
it will disappear outside the {}?


That is not strange, it actually makes a lot of sense. And it is, in fact, how the language works.

cout<<i<<; // is there any error?


Yes, that will error. 'i' no longer exists because it is no longer in scope.
Topic archived. No new replies allowed.