www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/07/23:38:03

From: "Stefan Schimanski" <stefan DOT schimanski AT metronet DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: template classes with DJGPP
Date: 4 Aug 1997 20:11:46 GMT
Organization: Metronet Kommunikationsdienste GmbH & Co. KG, Germany
Lines: 174
Message-ID: <01bca10d$bb446600$0100a8c0@stefan.schimanski>
NNTP-Posting-Host: braunschweig2.pop.metronet.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi,

I've implemented the following template class, that simulates a dynamic
array. Although I've put the whole definition of the class in the header
file, the linker displays errors. The compiler works fine. The linker can't
find the constructor definition:

Error: contest.o: In function `global constructors keyed to C':
contest.cc(24) Error: undefined reference to
`CContainer<int>::CContainer(int)'
There were some errors

I've removed the parameter of the constructor an tried again. But I got the
same error. I know that some parameter for gcc exist to control the way it
handles template. Would some of them help?

-------------------------------------------------------------------

/*
 CONTEST.CC
*/


#include <iostream.h>
#include "contain.h"


CContainer<int> C(16);


void main()
{
  for (int a=0; a<10; a++)
    C.Insert(a);

  for (int b=9; b>=0; b++)
    cout << C[b] << endl;
}

-------------------------------------------------------------------

/*
 CONTAIN.H
*/

#ifndef CONTAIN_H_INCLUDED
#define CONTAIN_H_INCLUDED

#include <string.h>


template <class T>
class CContainer
{
 public:
  CContainer(int aMin=4);
  virtual ~CContainer();

  int Insert(T aItem);
  void Remove(int aItem);

 protected:
  T *First;
  int NextItem;
  int Min;
  int Size;

  void CheckSize();
  void ChangeSize(int aSize);

 public:

  inline T operator[](int i) const
  {
    return *(First+i);
  }
};


//==========================================================


template <class T>
CContainer<T>::CContainer(int aMin)
{
  NextItem = 0;
  Size = 0;
  First = NULL;
  Min = 4;

  ChangeSize(Min);
}


template <class T>
CContainer<T>::~CContainer()
{
  delete[] First;
}


template <class T>
int CContainer<T>::Insert(T aItem)
{
  *(First+NextItem) = aItem;
  int Num = NextItem;
  NextItem++;

  CheckSize();

  return Num;
}


template <class T>
void CContainer<T>::Remove(int aItem)
{
  *(First+aItem) = NULL;

  if (NextItem-1 == aItem)
  {
    NextItem--;
  }

  CheckSize();
}


template <class T>
void CContainer<T>::CheckSize()
{
  if (NextItem>=Size)
  {
    ChangeSize(Size*2);
  } else
  if (NextItem < (Size>>1))
  {
    ChangeSize(Size >> 1);
  }
}


template <class T>
void CContainer<T>::ChangeSize(int aSize)
{
  // Check new size
  int NewSize = aSize;
  if (NewSize<Min) NewSize = Min;

  // Create new
  T *NewFirst = new T[Size];
  memset(First, 0, NewSize*sizeof(T));
  memmove(NewFirst, First, NextItem*sizeof(T));

  // Delete old and use new
  if (First!=NULL) delete[] First;

  First = NewFirst;
  Size = NewSize;
}


#endif
-- 

                         __  ___  ____  ____  ____  _  _ 
                        /  )/ __)(_  _)( ___)(_  _)( \( )
                         )( \__ \  )(   )__)  _)(_  )  ( 
                        (__)(___/ (__) (____)(____)(_)\_)
----------------------------------------------------------------------------
-----
1Stein-Software - http://www.geocities.com/SiliconValley/Way/1280 -
1Stein AT gmx DOT de
                    Everything is possible, at least in theory

- Raw text -


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