[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

template error in friend declaration



Hi, Is it a bug ??
I tried to compile a cpp program which ran correctly on previous
release (sarge debian, gcc compiler 3 ), it gave errors, for the
declaration  (even if I omit the symbols <> )

friend ostream & operator <<  <> (ostream & os, Vector  <T> & tab);

for other declaration ( not friend), the + operator,  it works !! here
is a complete example with errors at the compilation step.
If I omit the << operator, the program will run
Thank you for help


It works on previous compiler
-----------------------PROGRAM ------------------

#include <iostream>
using namespace std;
#define SIZE 10

template <class T>
class Vector{
protected:
 int size;
 T  ptrVect[SIZE];
public:
 Vector (int);
 Vector(int , T a[]);
 Vector <T>  operator + (  Vector <T> & tab);
 T & operator [] (int i);
 void print(){
   for (int i=0; i < size; i++)
     cout <<"  "<< ptrVect[i];
 }
 friend ostream & operator <<  <> (ostream & os, Vector  <T> & tab);
};
template <class T>
Vector<T>::Vector(int nbre, T a[]){
 size = nbre;
 for (int i=0; i < size;i++)
   ptrVect[i]= a [i];
}
template <class T>
Vector<T>::Vector(int nbre){
 size = nbre;
}

template <class T>
T & Vector<T>::operator [] (int i){
 return ptrVect [i];
}
template <class T>
Vector<T>  Vector<T>::operator + ( Vector  <T> & tab){
 Vector<T> v(size);
 v.size=size;
 for(int i=0; i < size; i++){
   v.ptrVect[i] = ptrVect[i] + tab.ptrVect[i];
 }
 return v;
}
template <class T>
ostream & operator  <<  (ostream & os, Vector  <T>& tab){
 for(int i=0; i < tab.size;i++)
   os << tab[i] <<" ";
 return os;
}
int  main(){
 int A[]={14, 20,26 }, B[]={9,7,34};
 Vector <int> v1(3, A), v2(3,B), v3(3);
 v3= v1+v2;
 cout << "\n first vector is   ";
 v1.print();
 cout << "\n second one is   ";
 v2.print();
 cout << "\n Their sum is  " ;
 cout << v3;
}



Reply to: