简体   繁体   中英

Creating an Array of Objects - Inheriting from a Class Template and using Constructor in C++

So here's my template class declaration:

template <class elemType>
class listType

I have a constructor like this:

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;

}

With a protected member variable like this:

elemType *list;

This is to pass in objects of type stockType in my code. I inherited a class from this template class listType called stockListType and tried to make a constructor that would pass in the parameters to the already made constructor in listType with this:

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

}

I'm not sure if I understand how to use class templates and constructors with class templates that I inherited a class from.

I tried making 5 objects of type stockType (inputting their information for their member variables using a file) and then trying to use the constructor of the inherited class with those objects in my main code:

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

But I keep getting an error when it tries to run.

EDIT: The error I get is Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

The child class header is:

#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 */

And the .cpp file of the child class is:

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

}

EDIT 3:

Thank you all for helping me, I figured out it was because I hadn't initialized list or my sortIndicesGainLoss.

Now I am getting an error under my sortList method. Does anybody have a clue as to why?

elemType *list; is not initialized. I really think that's the problem right there. try initializing it in the constructor to something like

list = new elemType [5]; since you'll be using 5 elements.

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;

}

The problem is, in the constructor of listType you are not allocating memory for the array list . If your class has a member of type * elemType , this would be a pointer to a elemType , but it does not mean it points to allocated memory. A solution to your problem is to write the constructor of listType as follows:

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;

}

But then do not forget to deallocate list when your object gets destructed. You need a distructor inside the definition of the class list as:

virtual ~listType { delete[] list; }

The destructor should be virtual, see the discussion here

That said, rather than using C-style arrays, if the size of the array list is known at compile time, I would rather suggest to use the C++11 arrays. So, in the declaration of the class listType your protected member list should be declared as

std::array<elemType, 5> list;

Then you do not need anymore to allocate and deallocate "manually" the array list . Also, you need to #include <array>

As for the second error when sortIndicesGainLoss = list; : you do not need the member stockType *sortIndicesGainLoss . In fact, by calling the constructor of the base class listType , you have already initialized the array list of elemType , which, being protected, is accessible to stockListType . So to solve the problem:

  • Remove stockType *sortIndicesGainLoss from the declaration of stockListType

  • In the cpp file remove sortIndicesGainLoss = list; , and everywhere use the inherited member list instead of sortIndicesGainLoss

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM