delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/25/05:29:24

From: William Heymann <heymann AT ucsu DOT Colorado DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Rhide and templates
Date: Thu, 24 Apr 1997 17:11:59 -0400
Organization: University of Colorado at Boulder
Lines: 86
Message-ID: <335FCC9F.5852@ucsu.Colorado.edu>
Reply-To: heymann AT ucsu DOT Colorado DOT edu
NNTP-Posting-Host: tele-anx0304.colorado.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I am currently working on using a template in my program to make a
dynamic 1 and 2 dimensional array of any type. Whenever I try to compile
the program the program will compile but, when it tries to link the
program into an executable I get unrecognized fuction errors for every
single function that I have that is part of the template. 
	I tried originally having two files a cpp and an h file but, when I
changed it to a template it didn't work. I looked at some examples in
some C++ books that I have and they had everything in the header file so
I put everything in my header file also. This still did not work. Even
the books example did not work.
	Is there a specific way that I have to include files to make them
templates or some syntax that I am missing. Included below is a small
example that shows the code for the 1D dynamic array.

main.cpp
#include "array.h"

void main(void)
{
ARRAY<int>   temp(10);

cout << "Width " << temp.Width() << endl;
temp.Enter(10, 5);
cout << "Result " << temp.Return(10) << endl;
};


array.h

#ifndef ARRAY_H
#define ARRAY_H


#include <iostream.h>


template <class Type>
class ARRAY

   {
   public:

   ARRAY(int width_);
   void Enter(int width_, Type c);
   Type Return(int width_);
   int Width(void);
   int Height(void);

   private:

   int width_;
   int height_;
   int width;
   int height;
   Type c;
   Type *array;
   };

    
   template <class Type>
   ARRAY<Type>::ARRAY(int width_)
   {
   width = width_;
   array = new Type [width];
   assert(array != 0);
   }

   template <class Type>
   void ARRAY<Type>::Enter(int width_, Type c)
   {
   array[width_ - 1] = c;
   }

   template <class Type>
   Type ARRAY<Type>::Return(int width_)
   {
   return array[width_ - 1];
   }

   template <class Type>
   int ARRAY<Type>::Width(void)
   {
   return width;
   }

#endif

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019