C++ 创建对象数组-从类模板继承并在C+中使用构造函数+;

C++ 创建对象数组-从类模板继承并在C+中使用构造函数+;,c++,class,templates,inheritance,constructor,C++,Class,Templates,Inheritance,Constructor,下面是我的模板类声明: template <class elemType> class listType 使用如下受保护的成员变量: listType(const elemType &, const elemType &, const elemType &, const elemType &, const elemType &){ list[0] = a; list[1] = b; list[2] = c; list[3] = d; li

下面是我的模板类声明:

template <class elemType>
class listType
使用如下受保护的成员变量:

listType(const elemType &, const elemType &, const elemType &, 
const elemType &, const elemType &){

list[0] = a;
list[1] = b;
list[2] = c;
list[3] = d;
list[4] = e;

}
elemType *list;
这是在我的代码中传入stockType类型的对象。我从该模板类listType继承了一个名为stockListType的类,并尝试创建一个构造函数,该构造函数将参数传递给listType中已创建的构造函数,如下所示:

stockListType :: stockListType(const stockType &a, const 
stockType &b, const stockType &c, const stockType &d, const 
stockType &e) : listType(a, b, c, d, e) {

}
我不确定我是否理解如何将类模板和构造函数与继承类的类模板一起使用

我尝试创建stockType类型的5个对象(使用文件输入其成员变量的信息),然后尝试在主代码中使用继承类的构造函数和这些对象:

stockListType object(obj1, obj2, obj3, obj4, obj5); 
但当它试图运行时,我不断收到一个错误

编辑: 我得到的错误是线程1:EXC\u BAD\u访问(代码=1,地址=0x0)

子类标题为:

#ifndef stockListTypeHeader_h
#define stockListTypeHeader_h

#include "listType Header.h"

class stockListType : public listType <class stockType>
{
public:
stockListType(const stockType &, const stockType &, const stockType &, const 
stockType &, const stockType &);

void sortList();
void swap(stockType&, stockType&);

const void printList();

protected:
    stockType *sortIndicesGainLoss;




};


#endif /* stockListTypeHeader_h */
\ifndef stocklistypeheader\u h
#定义stockListTypeHeader\u h
#包括“listType头.h”
类stockListType:公共listType
{
公众:
stockListType(常量stockType&,常量stockType&,常量stockType&,常量
股票类型&,常数股票类型&);
void sortList();
无效掉期(股票类型&,股票类型&);
const void printList();
受保护的:
股票类型*sortIndicesGainLoss;
};
#endif/*stockListTypeHeader\u h*/
子类的.cpp文件是:

#include <stdio.h>
#include "stockListTypeHeader.h"
#include "stockType.h"
#include <iostream>


void stockListType:: sortList(){
    sortIndicesGainLoss = list;

for(int i =0; i<5; i++){
    for(int j =0; j<5-i-1; j++) {
        if (sortIndicesGainLoss[j].getStockSymbol() > 
sortIndicesGainLoss[j+1].getStockSymbol()){
            swap(sortIndicesGainLoss[j], sortIndicesGainLoss[j+1] );
        }
        }
    }
}

void stockListType:: swap(stockType &xp, stockType &yp){
stockType temp = xp;
xp = yp;
yp = temp;

}

 void const stockListType:: printList() {
     for(int i=0; i<5; i++)
         cout << sortIndicesGainLoss[i];

}

 stockListType :: stockListType(const stockType &a, const stockType &b, const 
stockType &c, const stockType &d, const stockType &e) : listType(a, b, c, d, e) 
{

}
#包括
#包括“stockListTypeHeader.h”
#包括“stockType.h”
#包括
作废stockListType::sortList(){
sortIndicesGainLoss=列表;

for(int i=0;i
elemType*list;
未初始化。我真的认为这就是问题所在。请尝试在构造函数中将其初始化为

list=newelemtype[5];
因为您将使用5个元素

listType(const elemType &, const elemType &, const elemType &, 
const elemType &, const elemType &){

    this->list = new elemType [5];

    list[0] = a;
    list[1] = b;
    list[2] = c;
    list[3] = d;
    list[4] = e;

}

问题是,在
listType
的构造函数中,您没有为数组
list
分配内存。如果您的类有
*elemType
类型的成员,这将是指向
elemType
的指针,但这并不意味着它指向已分配的内存。 您的问题的解决方案是编写
listType
的构造函数,如下所示:

listType(const elemType &, const elemType &, const elemType &, 
const elemType &, const elemType &) : list(new elemType[5]) {

list[0] = a;
list[1] = b;
list[2] = c;
list[3] = d;
list[4] = e;

}
但是,当对象被销毁时,不要忘记取消分配
list
。在类
list
的定义中需要一个distructor,如下所示:

virtual ~listType { delete[] list; }
析构函数应该是虚拟的,请参见讨论

也就是说,如果在编译时知道数组
list
的大小,我建议使用C++11数组,而不是使用C样式的数组

std::array<elemType, 5> list;
std::数组列表;
这样您就不需要再“手动”分配和取消分配数组
列表了

至于
sortIndicesGainLoss=list;
时的第二个错误:您不需要成员
stockType*sortIndicesGainLoss
。事实上,通过调用基类
listType
的构造函数,您已经初始化了
elemType
的数组
list
,该数组受保护,可供访问ode>stockListType
。要解决此问题:

  • stockListType

  • 在cpp文件中,删除
    sortIndicesGainLoss=list;
    ,并在任何地方使用继承的成员
    list
    ,而不是
    sortIndicesGainLoss


请阅读,以及。当然,请尝试创建一个来向我们展示。最后,请。“但当它尝试运行时,我一直收到一个错误。”什么错误?请回答这个问题,将完整的错误消息复制并粘贴到其中。我收到的错误是:线程1:EXC_BAD_ACCESS(代码=1,地址=0x0)@yksisarvine您有未初始化的指针,它们不指向有效内存-像您这样取消引用它们会调用未定义的行为。这与模板或继承无关。此外,您应该更喜欢
std::vector
,而不是指针“我认为您可以创建一个指针,并根据需要将其索引分配给对象"-你从哪里学来的?这是完全错误的,任何体面的课程或教科书都应该对此进行解释。现在,我在我所做的这一点上遇到了一个错误:sortIndicesGainLoss=list;尝试不使用
sortIndicesGainLoss
。尝试删除
sortIndicesGainLoss=list;
并直接使用
list
。因此,更换每个insta
sortIndicesGainLoss
list
@JeJo也许Alexandra想在继续使用已经实现的库之前了解指针和手动内存管理。我们不应该根据编程方法来评判人们。我感谢@JeJo的帮助。老实说,你链接到的那篇文章让我很困惑(三/五法则)o.o.我会在有时间的时候学习如何使用向量,作为一名忙碌的大学生和其他事情,如圣经研究,我几乎没有足够的时间!谢谢你Marvinissacur也谢谢你的帮助。我现在对我的巫术列表有问题()方法。非常感谢。这真的很有帮助。所以当我使用C++11数组时,我不再需要使用delete[]了?从现在起,我必须使用delete[]。而且,现在我的sortList()方法下出现了一个错误。你知道为什么吗?@Alexandra:如果你将
list
声明为C++11,你就不需要使用
delete[]
。你应该把它看作一个普通变量,当它退出作用域时会被自动删除。关于
sortList()
中的错误,请先检查我更新的答案。@francesco我真的不明白你和其他人为什么要把std库扔给这个孩子。因为我认为这个孩子想先学习语言(因此他选择了c