A
alice
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 List\n";
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: Display\n2: Prepend\n3: Exit\n ";
cin>>choice;
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();
return 0;
}
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 List\n";
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: Display\n2: Prepend\n3: Exit\n ";
cin>>choice;
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();
return 0;
}