| re: Weird Output
"alice" <alice_vas2001@yahoo.com> wrote in message
news:1128023613.783413.258000@z14g2000cwz.googlegr oups.com...[color=blue]
> Hi,
> When I compiles the following program with g++, it gives the following
> output:
>
> [root@localhost C++]# g++ -o list list.C
> list.C: In function `int main()':
> list.C:116: jump to case label
> list.C:110: crosses initialization of `std::string data'
> list.C:120: jump to case label
> list.C:110: crosses initialization of `std::string data'[/color]
My Visual C++ documentation sums up the problem fairly well:
================================================== =======================
Compiler Error C2360
initialization of 'identifier' is skipped by 'case' label
The specified identifier initialization can be skipped in a switch
statement.
It is illegal to jump past a declaration with an initializer unless the
declaration is enclosed in a block.
The scope of the initialized variable lasts until the end of the switch
statement unless it is declared in an enclosed block within the switch
statement.
The following is an example of this error:
void func( void )
{
int x;
switch ( x )
{
case 0 :
int i = 1; // error, skipped by case 1
{ int j = 1; } // OK, initialized in enclosing block
case 1 :
int k = 1; // OK, initialization not skipped
}
}
================================================== =======================
Though it's not explicit in the code, the std::string object
does have an implicit initializer. (I.e. it's not possible
(by design) to define a string object without initializing
it -- lack of a specific initializer causes an empty string
to be created.
It works with 'int' because an 'int' object can be created
without initializing it (but I always recommend against doing that).
[color=blue]
>
>
> Internal compiler error: Error reporting routines re-entered.
> Please submit a full bug report,
> with preprocessed source if appropriate.
> See <URL:http://bugzilla.redhat.com/bugzilla/> for instructions.
> [root@localhost C++]#[/color]
Don't be concerned with this error unless it still occurs
after all the other errors have been fixed.
[color=blue]
>
> However, if I changes the template type to int in the statement
> LinkedList<string> list( i.e. to change the statement to
> LinkedList<int> list;),
> then then program compiles properly. Can anybody help to sort out the
> error.
>
> Here is the complete program:
>
> using namespace std;[/color]
You need to move this line to after the #include
directives. At this point there's no namespace 'std'.
[color=blue]
> #include <iostream>
> #include <string>[/color]
using namespace std;
[color=blue]
>[/color]
[snip][color=blue]
> switch(choice)
> {
> case 1:
> list.display();
> break;
>
> case 2:
> string data;
> cout<<"enter the data to be added to the list\n";
> cin>>data;
> list.Append(data);
> break;
>
> case 3:
> exit(1);
> break;
>
> default:
> cout<<"Invalid Choice\n";
> break;
>
> }
>
> main();[/color]
What is this for? It appears to be an attempt to have
'main()' call itself (recursion, and with no way to
terminate the recursion). Also, AFAIK, it's not legal
for 'main()' to call itself in C++.
[color=blue]
> return 0;
> }[/color]
-Mike |