🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1269

template forward declarations

#1269
From: Jim Dai
Date: Tue, 15 Dec 1992 19:47
131 lines
2991 bytes
I know you are still working on template support for g++.
This may not be a bug. But I am reporting it anyway.

I am basically copy down an template class example from
Lippman's book (2nd ed.). 
The forward declaration has to use exact arguments. If I
omit "os" and "ar" in the following, the compiler complains.

template <class Type> ostream&
  operator<<(ostream& os, array<Type>& ar);

I am using 
/usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.2/specs
gcc version 2.3.2

Codes are included below. Thanks for your excellent work.

Jim Dai

::::::::::::::
a_tst.cc
::::::::::::::
#include "array.h"
/* Template definitions can not split into more than one files.
 * I have to use include here.
 */

#include "array.cc" 

int main()
{
  int a[] = { 2, 3, 4, 5};
  array<int> b(a, 4);
  cout << b;
  return 0;
}

::::::::::::::
array.h
::::::::::::::
#ifndef _array_h
#define _array_h
#include <iostream.h>

template <class Type> class array;

template <class Type> ostream&
  operator<<(ostream& os, array<Type>& ar);

template <class Type> 
class array {
 public: 
  array(int sz) { init(0, sz); }
  array(const Type *ar, int sz) { init(ar, sz); }
  array(const array & iA) { init(iA.ia, iA.pSize);}
  virtual ~array() { delete [] ia; }
  
  array& operator = (const array&);
  void print(ostream&);
  int size() { return pSize; } 
  
  virtual Type& operator[](int ix) { return ia[ix]; }
  
 protected:
  void swap (int, int);
  void init(const Type*, int);
  
  int pSize;
  Type *ia;

};

  
#endif
::::::::::::::
array.cc
::::::::::::::
// #ifndef _array_cc 
// #define _array_cc

#include "array.h"
#include <assert.h>
#include <iostream.h>

template <class Type>
void  array<Type>::init(const Type *ar, int sz)
{
  ia = new Type[pSize=sz];
  assert (ia!= 0);
  
  for (int ix=0; ix<pSize;  ix++)
    ia[ix] = (ar!=0) ? ar[ix] : 0;
}

template <class Type>
array<Type>&  array<Type>::operator=(const array<Type> & iA)
{
  if (this == &iA) return *this;
  
  delete ia;
  init(iA.ia, iA.pSize);
  return *this;
}

template <class Type>
void array<Type>::print(ostream& os)
{

  const lineLength = 12;
  
  os << "( " << pSize << " )< ";
  for (int ix=0; ix <pSize; ix++) {
    if (ix % lineLength == 0 && ix) os << "\n\t";
    os << ia[ix];

    if (ix % lineLength != lineLength -1 &&
	ix != pSize -1) os << ", ";
  }
  os << " >\n";
}

template <class Type> ostream &
operator << ( ostream& os, array<Type>& ar)
{
  ar.print(os);
  return os;
}



// #endif
Technical Details
Message-ID: <9212151947.AA27863@chmsr.isye.gatech.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!chmsr.isye.gatech.edu!dai

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!chmsr.isye.gatech.edu!dai
From: dai@chmsr.isye.gatech.edu (Jim Dai)
Subject: template forward declarations
Message-ID: <9212151947.AA27863@chmsr.isye.gatech.edu>
Sender: gnulists@ai.mit.edu
Organization: GNUs Not Usenet
Distribution: gnu
Date: Tue, 15 Dec 1992 19:47:44 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 132