delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/08/03:28:21

Posted-Date: Sun, 8 Jun 1997 10:26:41 +0300
Message-ID: <01BB5524.FFB8B7E0@pl5.techno-link.com>
From: "Alian Ltd." <alian AT plovdiv DOT techno-link DOT com>
To: "'djgpp AT delorie DOT com'" <djgpp AT delorie DOT com>
Subject: RE: Template problem.
Date: Sat, 8 Jun 1996 10:25:51 +0300
MIME-Version: 1.0

Try removing the 'void' inside the Stack destructor parameter list. I'm not sure if it's gonna help, but once I think I had common problem. Usually the destructors do not have parameter lists and some compilers 'scream' when they see this.

BTW, why you need in NodeC destructor to make Prev=Next=NULL ? Is that necessary (anyway, you'll never use these member variables - you do not need to assign a value to them).

Regards !

Dony

-----Original Message-----
From:	Gregary J Boyles [SMTP:boylesgj AT lion DOT cs DOT latrobe DOT edu DOT au]
Sent:	Saturday, May 31, 1997 11:52 AM
To:	djgpp AT delorie DOT com
Subject:	Template problem.

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