0

I have a problem. I have implemented a Queue, while using a Class "Queue" and a Class "Element". the problem i have now is, that I can't work out how to create the template for class Element.

If I don't use the template and just use int instead of T. Everything works fine. I already looked for many examples on the Internet. But nobody uses two classes, which is probably more efficient. I think my problem is that i don't know how to use the pointer in templates.

PS: The template in Queue.h and.cpp works i think, but if i start trying to create a template for Element it doesn't work.

My Queue.h file

#ifndef ELEMENT_H
#define ELEMENT_H
#include "Element.h"


template <class T>
class Queue{
    public:
        explicit Queue(int max_queue);
        ~Queue() = default;
    
        void enqueue(T inhalt);
        Element* dequeue();
        Element* show();
        bool isEmpty();

        
    private:
        Element<T>* front{};
        Element<T>* back{};
        int max;
        int anzahl = 0;

};
#endif

My Queue.cpp file

#include "Queue.h"
#include <iostream>
#include <string>

template <class T>
Queue<T>::Queue(int max_queue){
    max = max_queue;
}

template <class T>
void Queue<T>::enqueue(T inhalt){
    Element* e = new Element(inhalt);
    if(max > anzahl){   
        if(isEmpty()){
            front = back = e;
        }else{
            back->setNext(e);
            back = e;
        }
    }   anzahl++;
}


template <class T>
Element* Queue<T>::dequeue(){
    Element* e = front;
    front = front->getNext();
    e->setNext(nullptr);
    return e;
}

template <class T>
bool Queue<T>::isEmpty()
{
    return anzahl == 0;
}


template <class T>
Element* Queue<T>::show()
{
    return front;
}

My Element.h file

#ifndef QUEUE_H
#define QUEUE_H

class Element{
    public:
        explicit Element(int);
        ~Element() = default;
        
        int getInhalt()const;
        void setInhalt(int);
        Element*getNext()const;
        void setNext(Element*);
        
    protected:
        int inhalt;
        Element* next;
};  
#endif

My Element.cpp file

#include <string>
#include "Element.h"


Element::Element( int inhalt_element )
{
    inhalt = inhalt_element;
    next = nullptr;
}

int Element::getInhalt() const {
    return inhalt;
}

void Element::setInhalt(int inhalt) {
    Element::inhalt = inhalt;
}

Element* Element::getNext() const {
    return next;
}

void Element::setNext(Element *next) {
    Element::next = next;
}

The warning I'm getting is main.cpp:(.text+0x1a): undefined reference to `Queue::Queue(int)' And If I try to use a template for the element class. There are hundreds of lines in the warnings, so I know I'm thinking completely wrong.

I am still pretty novice if it is about programming so any help or any idea would be really appreciated.

Thank you

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Proven
  • 3
  • 1
  • 1
    Read this: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – WhozCraig Jan 06 '22 at 09:45

1 Answers1

0

The implementation of template classes must be done in the .h The following code compiles.

#include <iostream>
#include <string>

template <class T> 
class Element{
public:
    
Element( T inhalt_element ){
    inhalt = inhalt_element;
    next = nullptr;
}

T getInhalt() const {
    return inhalt;
}

void setInhalt(T inhalt) {
    this->inhalt = inhalt;
}

Element<T>* getNext() const {
    return next;
}

void setNext(Element<T> *next) {
    this->next = next;
}
        
protected:
    T inhalt;
    Element<T>* next;
};  

template <class T> 
class Queue{
  public:
  Queue(int max_queue){
      max = max_queue;
  }

  void enqueue(T inhalt){
      Element<T>* e = new Element<T>(inhalt);
      if(max > anzahl){   
          if(isEmpty()){
              front = back = e;
          }else{
              back->setNext(e);
              back = e;
          }
      }   anzahl++;
  }


  Element<T>* dequeue(){
      Element<T>* e = front;
      front = front->getNext();
      e->setNext(nullptr);
      return e;
  }

  bool isEmpty(){
      return anzahl == 0;
  }

  Element<T>* show(){
      return front;
  }
  
  private:
  Element<T>* front{};
  Element<T>* back{};
  int max;
  int anzahl = 0;
};

int main() {
  std::cout << "Hello World!";
  Queue<int> queue(10);
  return 0;
}
Jerome Demantke
  • 161
  • 1
  • 8