Internal compiler error
mikes@nilenet.com
mikes@nilenet.com
Sat Jan 29 22:52:00 GMT 2000
g++ -v reveals version: 2.95.2
I have a pIII 500 with redhat linux, all the latest updates from
updates.redhat.com as of Jan24.
I compiled like this: $ g++ 13-7.cpp -o 13-7
AND GOT THIS:
mylist.h: In method `void
List<Pair<int,basic_string<char,string_char_traits<char>,__default_alloc_template<true,0>
> > >::print_all() const':
13-7.cpp:50: instantiated from
`Set<int,basic_string<char,string_char_traits<char>,__default_alloc_template<true,0>
> >::print()'
13-7.cpp:66: instantiated from here
mylist.h:49: Internal compiler error.
mylist.h:49: Please submit a full bug report.
mylist.h:49: See
<URL: http://www.gnu.org/software/gcc/faq.html#bugreport > for
instructions.
mike prog$
It isn't more than 150 lines so I pasted it below, two files 13-7.cpp
and mylist.h
my main problem was struct Pair, I was trying to overload operator
<<() to work properly.
Please send me feedback on how to get operator<< to work for struct
Pair.
here is 13-7.cpp:
//----------------------------------------------------
#include <iostream>
#include <string>
#include "mylist.h"
using namespace std;
template<class key,class value> struct Pair;
template<class key,class value> class Set;
template<class key,class value>
struct Pair
{
key k;
value v;
template<class key2,class value2> friend ostream& operator<< (ostream&
ls, const Pair<key2,value2>& rs);
// {
// ls << rs.k << '\t' << rs.v;
// return ls;
// }
};
template<class key,class value>class Set
{
public:
void add(const key& new_key, const value& new_value);
void print();
private:
List<Pair<key,value> > _set;
};
template<class key,class value>
template<class key2,class value2>
ostream& operator<<(ostream& ls, const Pair<key2,value2>& rs)
{
ls << rs.k << '\t' << rs.v;
return ls;
}
template<class key,class value> void Set<key,value>::add(const key&
new_key, const value& new_value)
{
Pair<key,value> temp;
temp.k = new_key;
temp.v = new_value;
_set.add(temp);
}
template<class key,class value> void Set<key,value>::print()
{
_set.print_all();
}
int main()
{
Set<int,string> test;
int tempi = 0;
string temps;
for(int a = 0; a < 10; a++)
{
cout << "Enter an integer: ";
cin >> tempi;
cout << "Enter a string: ";
cin >> temps;
test.add(tempi,temps);
}
test.print();
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AND HERE IS mylist.h:
//-------------------------------------------------
#ifndef MYLIST_H
#define MYLIST_H
#include <iostream>
using namespace std;
template<class T> class List
{
struct Link
{
Link(const T& init_val);
Link* next;
Link* prev;
T data;
};
Link* first;
Link* last;
public:
List();
~List();
void add(const T& new_data);
void print_all() const;
};
template<class T>List<T>::Link::Link(const T&
init_val):data(init_val),next(0),prev(0){}
template<class T> List<T>::List()
{
first = 0;
last = 0;
}
template<class T> void List<T>::add(const T& new_data)
{
Link* new_link = new Link(new_data);
if(!last)
last = new_link;
else
first->prev = new_link;
new_link->next = first;
first = new_link;
}
template<class T>void List<T>::print_all() const
{
Link* temp = first;
while(temp)
{
cout << temp->data << endl;
temp = temp->next;
}
}
template<class T>List<T>::~List()
{
Link* temp = first;
while(temp)
{
Link* temp2 = temp;
temp = temp->next;
delete temp2;
}
}
#endif //ndef MYLIST_H
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
More information about the Gcc-bugs
mailing list