c++ - 模板继承问题

标签 c++ templates inheritance

我有一个抽象队列,我想将它的成员继承给另一个子类。当我尝试在我的主文件中创建子对象时,我不断得到对 `AbstractQueue::AbstractQueue()' 的 undefined reference

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

增量队列.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; 让编译器生成它, 但是这不一定是正确的(见评论)。

关于c++ - 模板继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52429359/

相关文章:

c++ - 将英镑转换为欧元 - 使用参数

通过可变模板继承 C++11 构造函数

c++ - 为什么这段代码不会产生编译错误?

c++ - 使用继承类从模板类继承

c++ - 从派生类中删除虚函数

c++ - Visual C++ 2010 错误。运行 LibICP 时系统找不到指定的文件

c++ - 为什么 FormatMessage 只为 ERROR_SYSTEM_PROCESS_TERMINATED 和 ERROR_UNHANDLED_EXCEPTION 系统错误创建部分消息?

c++ - 如何创建一个字符串数组来拆分一个字符,这些字符的单词由 ""分隔? C++

c++ - 有没有办法在 C++ 中使用模板函数来做到这一点

c++ - std::pair 具有可比较对象和不可比较对象需要排序