【问题标题】:Creating an Array of Objects - Inheriting from a Class Template and using Constructor in C++创建对象数组 - 从类模板继承并在 C++ 中使用构造函数
【发布时间】:2019-04-18 07:24:40
【问题描述】:

这是我的模板类声明:

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) {

}

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

我尝试制作 5 个 stockType 类型的对象(使用文件为成员变量输入它们的信息),然后尝试在我的主代码中将继承类的构造函数与这些对象一起使用:

stockListType object(obj1, obj2, obj3, obj4, obj5); 

但是当它尝试运行时我不断收到错误。

编辑: 我得到的错误是 Thread 1: EXC_BAD_ACCESS (code=1, address=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 */

而子类的.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) 
{

}

编辑 3:

感谢大家帮助我,我发现这是因为我没有初始化列表或我的 sortIndicesGainLoss。

现在我的 sortList 方法出现错误。有人知道为什么吗?

【问题讨论】:

  • "但是当它尝试运行时我一直收到错误。" 什么错误?请edit问题复制并粘贴完整的错误信息。
  • 我得到的错误是:线程 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
  • 你有未初始化的指针,它们没有指向有效的内存——像你一样取消引用它们会调用未定义的行为。这与模板或继承无关。此外,您应该更喜欢 std::vector 而不是为此的指针
  • “我认为你可以创建一个指针,然后将它的索引分配给你喜欢的对象” - 你从哪里学到的?这是完全错误的,任何像样的课程或教科书都应该解释它

标签: c++ class templates inheritance constructor


【解决方案1】:

elemType *list;初始化。我真的认为这就是问题所在。尝试在构造函数中将其初始化为类似

list = new elemType [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;

}

【讨论】:

  • 现在我遇到了一个错误:sortIndicesGainLoss = list;
  • 尽量不要使用sortIndicesGainLoss。尝试删除sortIndicesGainLoss = list; 并直接使用list
  • 所以用list替换sortIndicesGainLoss的每个实例
  • @JeJo 也许亚历山德拉想在继续使用已经实现的库之前了解指针和手动内存管理。我们不应该根据人们的编程方法来评判他们。
  • 感谢@JeJo 的帮助。老实说,你链接到的那篇文章让我感到困惑(三/五规则。)o.O.当我有时间的时候,我会学习如何使用向量,作为一个忙碌的大学生和其他事情,比如圣经学习,我几乎没有足够的时间!也感谢 marvinIsSacul 的所有帮助。由于某种原因,我的 sortList() 方法现在出现问题。
【解决方案2】:

问题是,在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 的定义中使用一个析构函数:

virtual ~listType { delete[] list; }

析构函数应该是虚拟的,见讨论here

也就是说,与其使用 C 样式的数组,如果数组 list 在编译时已知,我宁愿建议使用 C++11 数组。因此,在 listType 类的声明中,您的受保护成员 list 应声明为

std::array<elemType, 5> list;

然后您不再需要“手动”分配和解除分配数组list。另外,你需要#include &lt;array&gt;

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

  • stockListType的声明中删除stockType *sortIndicesGainLoss

  • 在cpp文件中删除sortIndicesGainLoss = list;,处处使用继承的成员list而不是sortIndicesGainLoss

【讨论】:

  • 非常感谢。这真的很有帮助。那么当我使用 C++11 数组时,我不需要再使用 delete [] 了吗?从现在开始我将不得不使用它。
  • 另外,现在我的 sortList() 方法出现错误。你知道为什么吗?
  • @Alexandra:如果您将 list 声明为 C++11,则不需要使用 delete[]。您应该将其视为普通变量,当它退出范围时会自动删除。对于sortList() 中的错误,请先查看我更新的答案。
  • @francesco 我真的不明白你和其他人为什么要把孩子扔给 std 库。因为我认为孩子想先学习语言(因此他创建了自己的算法),而不是库。随着时间的推移,他会开始学习它们。
  • @marvinIsSacul 我想我已经建议 both “手动”使用数组和自定义交换,或者使用 std 库....
猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2013-09-23
  • 2015-08-06
  • 1970-01-01
  • 2016-12-30
  • 2013-09-04
相关资源
最近更新 更多