Reputation: 41
I have an Abstractqueue and I want to inherit it's members to another child class. When I try to create the child object in my main file I keep getting an undefined reference to `AbstractQueue::AbstractQueue()'
enter code here
#ifndef ABSTRACT_QUEUE_H
#define ABSTRACT_QUEUE_H
#include <iostream>
template <class Type>
class AbstractQueue
{
protected:
Type *items;
int front;
int back;
int capacity;
int count;
private:
// data goes here
public:
AbstractQueue(int s);
AbstractQueue(void);
~AbstractQueue(void);
bool empty();
int size();
Type frontele(); //throw(exception) {}
Type dequeue(); //throw(exception) {}
void enqueue ( Type e );
};
template <class Type>
AbstractQueue<Type>::AbstractQueue(int s){
items = new Type[s];
front = 0;
back = 0;
capacity = s;
count = 0;
std::cout << "made the abstract queue!" << std::endl;
}
template <class Type>
AbstractQueue<Type>::~AbstractQueue() {
delete[] items;
std::cout << "destructor called" << std::endl;
}
#endif
IncrementalQueue.h
#ifndef _INCREMENTALQUEUE_H
#define _INCREMENTALQUEUE_H
#include "Queue.h"
//#define SIZE = 10
#include <iostream>
template <class Type>
class IncrementalQueue : public AbstractQueue<Type>{
public:
//AbstractQueue(void);
//~AbstractQueue(void);
IncrementalQueue(int s);
bool empty();
int size();
Type frontele(); //throw(exception) {}
Type dequeue(); //throw(exception) {}
void enqueue ( Type e );
//~IncrementalQueue(void);
//AbstractQueue(void);
//AbstractQueue(int size);
//bool empty(void) ;
/*if (count == 0) {
return true;
}
else {
return false;
}*/
//int size(void);
//Type front throw(exception) {}
//Type dequeue(); //throw(exception) {}
//void enqueue ( Type e ) ;
//IncrementalQueue(int size);
};
template <class Type>
IncrementalQueue<Type>::IncrementalQueue(int s){
this->items = new Type[50];
this->front = 0;
this->back = 0;
this->capacity = 50;
this->count = 0;
std::cout << "made the incremental queue!" << std::endl;
}
#endif
main.cpp
#include "Queue.h"
#include "IncrementalQueue.h"
int main(){
IncrementalQueue<int> incqueue(50);
return 0;
}
I'm a little rusty with templates so I've been struggling at it for a couple of hours. does anybody have any clues on where my code can be failing?
Upvotes: 0
Views: 42
Reputation: 18864
IncrementalQueue::IncrementalQueue()
constructor is calling the AbstractQueue::AbstractQueue()
constructor and you have not defined it.
To make it compile you can just say AbstractQueue() = default;
to have it generated by the compiler, if you are using C++11
or later, but that will not necessarily be correct (see the comments).
Upvotes: 2
Reputation: 1820
As it always happens with templates, you either need to put AbstractQueue::AbstractQueue()
implementation (function body) to its header, or, if you know in advance all its possible template parameters, explicitly instantiate this function in cpp-file:
template class AbstractQueue<NeededType>;
In your case, it's impossible to know all possible template args in advance, so you need to put your function body to the header. That's the price of separate unit compilation.
Upvotes: 0