Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中带有泛型的抽象类+;/Qt_C++_Qt_Generics_Abstract Class - Fatal编程技术网

C++ c+中带有泛型的抽象类+;/Qt

C++ c+中带有泛型的抽象类+;/Qt,c++,qt,generics,abstract-class,C++,Qt,Generics,Abstract Class,当我运行下面的代码并按下enter按钮时,我得到一个分段错误。 我在互联网上到处都搜索过,但我找不到问题所在。我对C++/Qt很陌生 基类: stack.h 计算器 // ... void calculator::on_b_enter_clicked() { h->pop(); } // ... 错误: 由于收到来自操作系统的信号,下位机停止 信号名称:SIGSEGV 信号含义:分段故障此代码: A.h 模板 甲级 { 公众: A(){} 虚空f1

当我运行下面的代码并按下enter按钮时,我得到一个分段错误。 我在互联网上到处都搜索过,但我找不到问题所在。我对C++/Qt很陌生

基类:
stack.h

计算器

// ...    
void calculator::on_b_enter_clicked()
    {
        h->pop();
    }
// ...
错误:
由于收到来自操作系统的信号,下位机停止
信号名称:SIGSEGV
信号含义:分段故障

此代码:

A.h

模板
甲级
{
公众:
A(){}
虚空f1()=0;
};
模板
B类:公共A
{
公众:
B(){}
void f1(){}
};
main.cpp

#include "A.h"
int main ()
{
    A<int> *a = new B<int>();

    a->f1();
}
#包括“A.h”
int main()
{
A*A=新的B();
a->f1();
}
编译并工作,因为所有模板函数都在头文件中定义。如果要拆分声明和定义,可以使用以下方法之一:

  • 在头文件的底部包括cpp文件
  • 将cpp文件包括在main.cpp中

我很惊讶它能够编译,因为您已经在源文件中定义了头文件。但如果它以某种方式出现,那么可能您没有正确初始化
h
。或者可能是你不知何故破坏了它的价值。使用调试器检查它是否指向有效对象。能否显示代码部分,即您实际创建对象的位置(使用
new
等)?(对不起,我的意思是“在源文件中定义了模板”。现在更正它为时已晚。)@MatthiasB我尝试使用new关键字。。但它给出了错误:未定义对“arraystack::arraystack()”的引用。还有@mike,你在哪里看到我在源文件中定义了头文件?好的,我们尝试在头文件中定义模板。。没有成功。。你能在回答中给出正确的例子吗?为什么A*A=new B()?是B*a=新的B();错误?不,A是B的基类,因此您可以创建B实例并将其分配给A指针。然后可以使用A接口
#ifndef ARRAYSTACK_H
#define ARRAYSTACK_H

#include "stack.h"

template <class T> class arraystack : public stack<T>
{
public:
    arraystack();
    T pop();
    void push(T i);
};

#endif // ARRAYSTACK_H
#include "arraystack.h"
#include <QDebug>

template<class T> arraystack<T>::arraystack()
{
}

template<class T> T arraystack<T>::pop(){
    qDebug() << "popping bad";
}

template<class T> void arraystack<T>::push(T i){
    qDebug() << "pushing shit";
}
// ...
private:
    Ui::calculator *ui;
    arraystack<int> *h;
    bool integer;
// ...
// ...    
void calculator::on_b_enter_clicked()
    {
        h->pop();
    }
// ...
template <typename T>
class A
{
public:
    A(){}

    virtual void f1() = 0;
};

template <typename T>
class B: public A<T>
{
public:
    B(){}

    void f1(){}
};
#include "A.h"
int main ()
{
    A<int> *a = new B<int>();

    a->f1();
}