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?