Thread View: gnu.g++.help
1 messages
1 total messages
Started by Karsten Pahnke
Fri, 09 Oct 1992 16:01
global operator<< for templates
Author: Karsten Pahnke
Date: Fri, 09 Oct 1992 16:01
Date: Fri, 09 Oct 1992 16:01
86 lines
2759 bytes
2759 bytes
/*
Hallo,
it seems to be there is an error in g++ 2.2.2
it is not possible to implement global(not friend)
ostream operators for template classes
the following is a shorthand example of the problem
I thank for your efforts
Karsten
karsten@Inf-Technik.TH-Ilmenau.DE
-----------------------------------------------------------
*/
#include <iostream.h>
template <class T>
class Array {
T* _arr;
int _len;
public:
Array(int len) { _arr = new T[_len = len]; }
int len() const { return _len; }
T& operator[](int i) {return _arr[i]; }
void print(ostream& s) const
{
for ( int i = 0; i < _len; i++ ) {
s << _arr[i] << " ";
}
}
/* version 1 works properly, but friend is not necesssary */
/* thats why version 2 */
/*
friend ostream& operator<<(ostream& s, const Array& a)
{ a.print(s); return s; }
*/
};
/* works properly */
template <class T>
inline int empty(const Array<T>& a)
{
return (a.len() == 0);
}
/* version 2 works with cfront, but not with gnu */
/*
/space/localtmp/gnu/bin/g++ -B/space/localtmp/gnu/lib/gcc/sparc-sun-sunos4.1.1/2.2.2/
-I/space/local/gnu/lib/g++-include -g -DPRECOND -DYYDEBUG -DDEBUG \
-I/home/usr/omsi/work/inc \
-c comp_out.cc
comp_out.cc: In function `int main ()':
comp_out.cc:42: warning: dbx info for template class methods not yet supported
/space/localtmp/gnu/bin/gcc: Internal compiler error: program cc1plus got fatal signal 11
*** Error code 1
make: Fatal error: Command failed for target `comp_out.o'
*/
template <class T>
inline ostream& operator<<(ostream& s, const Array<T>& a)
{
a.print(s); return s;
}
main()
{
Array<int> a(10);
cerr << empty(a) << endl;
cerr << a << endl;
}
--
-----------------------------------------------------------
| Dipl.-Ing. Karsten Pahnke | Telefon : 0037 672 691167 |
| Technische Hochschule Ilmenau | 0037 672 691168 |
| PSF 327, O-6300 Ilmenau | Telefax : 0037 672 691517 |
|-----------------------------------------------------------|
| E-Mail : Pahnke@Inf-Technik.TH-Ilmenau.DE |
-----------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads