模板继承问题
Template Inheritance issues
我有一个抽象队列,我想把它的成员继承给另一个子类。当我尝试在我的主文件中创建子对象时,我不断得到对"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
主.cpp
#include "Queue.h"
#include "IncrementalQueue.h"
int main(){
IncrementalQueue<int> incqueue(50);
return 0;
}
我对模板有点生疏,所以我已经挣扎了几个小时。 有没有人知道我的代码在哪里会失败?
IncrementalQueue::IncrementalQueue()
构造函数正在调用AbstractQueue::AbstractQueue()
构造函数,而您尚未定义它。
要使其编译,如果您使用的是C++11
或更高版本,您可以说AbstractQueue() = default;
由编译器生成它,但这不一定是正确的(请参阅注释(。
就像模板总是发生的那样,你需要将AbstractQueue::AbstractQueue()
实现(函数体(放到它的头上,或者,如果你事先知道它所有可能的模板参数,在 cpp-file 中显式实例化这个函数:
template class AbstractQueue<NeededType>;
在您的情况下,不可能提前知道所有可能的模板参数,因此您需要将函数体放在标头中。这就是单独单元编译的代价。
相关文章:
- 混合组合和继承的C++问题
- 运算符继承和 cpp 核心准则 c.128 的问题
- 类继承,ENUM 与 AST 类实现的问题
- C++ 多级虚拟继承编译问题
- 如何解决此问题(基础模板和继承)
- 模板继承问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 继承此C++代码有什么问题
- 一个关于继承和引用的C++问题
- C++继承和"常量"交互的问题
- 在钻石问题的求解中,为什么要虚拟地继承两次grand-parent类
- C++的抽象类继承和构造函数的问题
- C++:继承和列表函数作为类成员的问题
- SIGSEGV 因为基/派生类的继承问题
- 关于异常继承中的语法的问题
- C++继承的类设计问题
- 关于继承和覆盖的问题
- C++ 构造函数问题 // 继承的类
- 对复制构造函数的c++ OOP程序问题?继承