🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

class templates

#1293
From: Rick Spada
Date: Thu, 17 Dec 1992 20:27
74 lines
2125 bytes
Enclosed is an example of a problem that I've run into using class templates.
Can anyone shed some light on this?

-- Rick Spada
   The MathWorks, Inc.
   ricks@mathworks.com

Using:
	gcc version 2.2.2
	GNU CPP version 2.2.2 (sparc)
	GNU C++ version 2.2.2 (sparc) compiled by GNU C version 2.2.2.

/*
 * Can anyone tell me what\'s wrong with this?  With THIS_COMPILES undef\'d, I
 * get error messages ofthe form:
 *
 * >    lipman.cc: In function `int  main ()':
 * >	lipman.cc:35: template for method `Array' doesn't match any in class `Array<int>'
 * >	lipman.cc:35: warning: dbx info for template class methods not yet supported
 * >	lipman.cc:35: template for method `Array' doesn't match any in class `Array<float>'
 */

# include <iostream.h>
# include <stdlib.h>

#define THIS_COMPILES
#undef THIS_COMPILES

template <class type>
class Array {
  public:
#if defined(THIS_COMPILES)
    Array(int sz = 12) : ia(new type[sz]), size(sz) { }
    ~Array(void) { delete ia; }
#else
    Array(int sz = 12);
    ~Array(void);
#endif
    
    inline type & operator[] (int elem) { return ia[elem]; }
    
    inline int Size(void) { return size; }
    
  private:
    type *ia;
    int size;
};

#if !defined(THIS_COMPILES)

template <class type>
Array<type>::Array (int sz) : ia(new type[sz]), size(sz) { }

template <class type>
Array<type>::~Array (void) { delete ia; }

#endif

main ()
{
    Array<int> ia;
	Array<float> fa;
    
    for (int i = 0; i < ia.Size (); i++) {
        ia[i] = rand ();
		fa[i] = rand ();
	}
    
    for (i = 0; i < ia.Size (); i++)
        cout << ia[i] << "\n";
    
    for (i = 0; i < fa.Size (); i++)
        cout << fa[i] << "\n";
}
Technical Details
Message-ID: <9212172027.AA05989@dogbert.Natick>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu![144.212.2.21]!ricks%dogbert

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu![144.212.2.21]!ricks%dogbert
From: ricks%dogbert@[144.212.2.21] (Rick Spada)
Subject: class templates
Message-ID: <9212172027.AA05989@dogbert.Natick>
Sender: gnulists@ai.mit.edu
Organization: GNUs Not Usenet
Distribution: gnu
Date: Thu, 17 Dec 1992 20:27:27 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 75