www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/03/15:34:12

From: boylesgj AT lion DOT cs DOT latrobe DOT edu DOT au (Gregary J Boyles)
Newsgroups: comp.os.msdos.djgpp
Subject: Template problem.
Date: 3 Jun 1997 15:20:04 GMT
Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
Lines: 234
Distribution: world
Message-ID: <5n1cn4$ns3@lion.cs.latrobe.edu.au>
NNTP-Posting-Host: lion.cs.latrobe.edu.au
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

What am I doing wrong with this friggin template class.?


gxx -fno-implicit-templates test.cc


test.o: In function `main':
test.cc:15: undefined reference to `StackQueueC<int>::StackQueueC(void)'
test.cc:30: undefined reference to `StackQueueC<int>::~StackQueueC(void)'


void main()
{
  StackQueueC<int> S;
}




template <class T>
class NodeC
{

  public:

    // The node's data.
    T Data;

    // Pointer to the next and previous nodes.
    NodeC<T> *Next,*Prev;

    // Constructor.
    NodeC()
    {
      Next=Prev=NULL;
    };

    // Destructor.
    ~NodeC()
    {
      Next=Prev=NULL;
    };

};




template <class T>
class StackQueueC
{

  private:

    // The first and last node in the linked list.
    NodeC<T> *First,*Last;

    // The number of nodes in the list.
    int NumNodes;

  public:

    // Constructor.
    StackQueueC(void);

    // Detsructor.
    ~StackQueueC(void);

    // Checks for an empty queue or stack.
    bool Empty();

    // Stack functions.
    bool Pop(T &Data);

    bool Push(T &Data);

    // Queue functions.
    bool Enqueue(T &Data);

    bool Dequeue(T &Data);

};




// Constructor.
template <class T>
StackQueueC<T>::StackQueueC()
{
  First=Last=NULL;
  NumNodes=0;
}




// Detsructor.
template <class T>
StackQueueC<T>::~StackQueueC()
{
  T Data;

  while (!Empty())
    Pop(Data);
}




// Checks for an empty queue or stack.
template <class T>
bool StackQueueC<T>::Empty()
{
  return NumNodes==0;
}




// Stack functions.
template <class T>
bool StackQueueC<T>::Pop(T &Data)
{
  NodeC<T> *Temp=NULL;
  bool Result=false;

  // If the stack is not empty.
  if (!Empty)
  {
    Temp=First;
    if (NumNodes==1)
    {
      Last=NULL;
      First=NULL;
    }
    else
    {
      First=Temp->Next;
      First->Prev=NULL;
    }
    Data=Temp->Data;
    delete Temp;
    NumNodes--;
    Result=true;
  }
  return Result;
}




template <class T>
bool StackQueueC<T>::Push(T &Data)
{
  NodeC<T> *Temp=NULL;
  bool Result=false;

  // Create a new node.
  Temp=new NodeC<T>;

  // If successful then add the node to the stack.
  if (Temp)
  {
    Temp->Data=Data;
    NumNodes++;
    Result=true;
    if (Empty())
    {
      Last=Temp;
      First=Temp;
    }
    else
    {
      Temp->Next=First;
      First->Prev=Temp;
      First=Temp;
    }
  }
  return Result;
}




// Queue functions.
template <class T>
bool StackQueueC<T>::Enqueue(T &Data)
{
  NodeC<T> *Temp=NULL;
  bool Result=false;

  // Create a new node.
  Temp=new NodeC<T>;

  // If successful then add the node to the queue.
  if (Temp)
  {
    Temp->Data=Data;
    NumNodes++;
    Result=true;
    if (Empty())
    {
      Last=Temp;
      First=Temp;
    }
    else
    {
      Last->Next=Temp;
      Temp->Prev=Last;
      Last=Temp;
    }
  }
  return Result;
}




template <class T>
bool StackQueueC<T>::Dequeue(T &Data)
{
  // Poping is the same as dequeuing
  return Pop(Data);
}




#endif




- Raw text -


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