what do these errors mean?

... and how do i fix them?
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
int main(int argc, char* argv[])
{
	switch(argc)
	{
		case 1:
			error("no input files", 0);
			break;
		case 2:
			ifstream script(argv[1]);

			if(script.good())
			{
				if(stdFile(argv[1]))
					parse(script);

				else
					error("invalid file format", 0);
			}

			else
				error("invalid file path", 0);

			script.close();
			break;
		default:
			error("too many arguements", 0);
	}
}



interpret.cpp: In function ‘int main(int, char**)’:
interpret.cpp:50:3: error: jump to case label
interpret.cpp:34:20: error:   crosses initialization of ‘std::ifstream script’
Show all the code.
You need to enclose the case in braces in order to create objects in it.
ne555 is correct.

1
2
3
4
5
6
7
8
9
10
11
	switch(argc)
	{
	//...
		case 2:
			ifstream script(argv[1]);  // <-  'script' constructed here
			//...
			break;
		default:
			// 'script' still in scope here, but not constructed here
			//...
	} // 'script' destructed here 


If the 'default' label is taken... 'script' will still be in scope for the 'default' section of code. However the program will have jumped passed the line where it was constructed, meaning you'd have access to an object that was not properly constructed. This is extremely dangerous!

Furthermore, 'script' will be destructed at the end of the switch statement. Trying to destruct something that was never constructed is also extremely dangerous and would likely cause the program to crash if it were allowed.


To fix you need to restrict where the object is constructed:

1
2
3
4
5
6
7
8
9

		case 2:
		{  // <- limits scope of script
			ifstream script(argv[1]);  // <-  'script' constructed here
			//...
			break;
		} // <- now it will be destructed here
		default:
			// no longer in scope here, so no error 
This question might sound very basic, but I was wondering why 'script' would be constructed when case 2 is not true?

There seems to be some basic concept that I seem to be missing here, I'd be grateful if you could explain.

Thanks!
This is a scope:
1
2
3
switch
{
}

But if you only construct script in case 2:, the compiler doesn't know what to do for the other cases.

To make it do what you expect, you need to give it a scope restricted to case 2 as in Disch's code above.

It's just a messy feature from where C++ meets C.
Last edited on
Thanks for the response kbw.

If it interests anyone, here is what I gathered from a little "googling".

Switch statements are just labels that instructs the compiler to jump to a particular part of code during execution, therefore all the variables not withtin {} belong to the scope of the function inside which the switch statement resides.

i.e., during the construction of a symbol table, the variables not in {} get declared with the scope as the entire function (or outer scope), and it should ideally have definition in the same scope.

However, the definition of the variable happens in the second run of compiling, i.e., after the symtab has already been constructed.

The code that defines the variable is within the bunch of statements after the case label but before the next label, which is why the variables get declared but not defined.

I'd appreciate it if anyone could point out something missing in what I've understood!

Thanks!
Topic archived. No new replies allowed.