C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Weird Output

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
alice
Guest





PostPosted: Thu Sep 29, 2005 7:53 pm    Post subject: Weird Output Reply with quote



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'


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++]#

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;
#include <iostream>
#include <string>

template <class T>
class ListElement
{
public:
ListElement(ListElement<T> *,T );
T const & Data() const;
ListElement<T> ** Next() ;

private:

ListElement<T> *next;
T data;
};

template <class T>
class LinkedList
{
public:
LinkedList();
void display();
void Append(T item);
private:
ListElement<T> *Head, *Tail;
};

// Implementing the various functions of the ListElement class.
template <class T>
ListElement<T>:: ListElement(ListElement<T> * _next, T _data):
data(_data),next(_next)
{
}

template <class T>
T const & ListElement<T>:: Data() const
{
return data;
}

template <class T>
ListElement<T> ** ListElement<T>:: Next()
{
return &next;
}

// ListElement class functions implemented.

//Implementing the various functions of the LinkedList class.

template <class T>
void LinkedList<T>:: display()
{
ListElement<T> *tmp;
tmp=Head;
if(Head == NULL)
{
cout<<"No elements in the Listn";
return;
}

while(tmp!=NULL)
{
cout<< tmp->Data() << "n";
tmp=*tmp->Next();
}
return ;
}

template <class T>
LinkedList<T>:: LinkedList()
:Head(NULL), Tail( NULL)
{
}

template <class T>
void LinkedList<T>:: Append (T item)
{
ListElement<T> *tmp;
tmp = new ListElement<T>(NULL, item);

if(Head == NULL){
Head = Tail = tmp;
return ;
}
*Tail->Next() = tmp;
Tail = tmp;
return;
}


LinkedList<string> list;

int main(void)
{

// LinkedList<string> list;
int choice;
cout<<"1: Displayn2: Prependn3: Exitn ";
cin>>choice;
switch(choice)
{
case 1:
list.display();
break;

case 2:
string data;
cout<<"enter the data to be added to the listn";
cin>>data;
list.Append(data);
break;

case 3:
exit(1);
break;

default:
cout<<"Invalid Choicen";
break;

}

main();
return 0;
}




Back to top
John Harrison
Guest





PostPosted: Thu Sep 29, 2005 8:11 pm    Post subject: Re: Weird Output Reply with quote



alice wrote:
Quote:
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'


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++]#

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.


int main(void)
{

// LinkedList<string> list;
int choice;
cout<<"1: Displayn2: Prependn3: Exitn ";
cin>>choice;
switch(choice)
{
case 1:
list.display();
break;

case 2:
string data;
cout<<"enter the data to be added to the listn";
cin>>data;
list.Append(data);
break;

case 3:
exit(1);
break;

default:
cout<<"Invalid Choicen";
break;


Imagine the program has reached this point. Now does the compiler
destroy the string variable 'data' or not? If you got here via case 1, 3
or default then no, but if you got here via case 2 then yes. This kind
of this is too much for the compiler to keep track of so it is forbidden.

Change case 2 to this

case 2:
{
string data;
cout<<"enter the data to be added to the listn";
cin>>data;
list.Append(data);
}
break;

Now only case 2 uses the data variable and the problem goes away.

john

Back to top
Mike Wahler
Guest





PostPosted: Thu Sep 29, 2005 8:13 pm    Post subject: Re: Weird Output Reply with quote




"alice" <alice_vas2001 (AT) yahoo (DOT) com> wrote

Quote:
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'

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).

Quote:


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++]#

Don't be concerned with this error unless it still occurs
after all the other errors have been fixed.

Quote:

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;

You need to move this line to after the #include
directives. At this point there's no namespace 'std'.

Quote:
#include <iostream
#include

using namespace std;

Quote:

[snip]
switch(choice)
{
case 1:
list.display();
break;

case 2:
string data;
cout<<"enter the data to be added to the listn";
cin>>data;
list.Append(data);
break;

case 3:
exit(1);
break;

default:
cout<<"Invalid Choicen";
break;

}

main();

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++.

Quote:
return 0;
}

-Mike




Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.