2

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?

14
  • 3
    Please read about how to ask good questions, as well as this question checklist. And of course please try to create a Minimal, Complete, and Verifiable Example to show us. Lastly please learn how to debug your programs. Commented Nov 15, 2018 at 8:38
  • 2
    "But I keep getting an error when it tries to run." What error? Please edit the question to copy and paste full error message into it. Commented Nov 15, 2018 at 8:39
  • 1
    The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen Commented Nov 15, 2018 at 8:45
  • 1
    You have uninitialized pointers, they don't point to valid memory - dereferencing them like you do invokes undefined behavior. That has nothing to do with templates or inheritance. Also you should prefer std::vector over pointers for this Commented Nov 15, 2018 at 8:51
  • 1
    "I thought you could create a pointer and just assign its indexes to objects as you pleased" - Where did you learn that? It's completely wrong and any decent course or textbook should be explaining it Commented Nov 15, 2018 at 8:52

2 Answers 2

2

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;

}
Sign up to request clarification or add additional context in comments.

5 Comments

Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
try not using sortIndicesGainLoss. try removing sortIndicesGainLoss = list; and use list directly.
so replace every instance of sortIndicesGainLoss with list
@JeJo perhaps Alexandra wants to learn about pointers, and manual memory management before going on to use already implemented libraries. We should not judge people on their methodology of programming.
I appreciate the help @JeJo. In all honesty, that article you linked to confuses me (Rule of three/five.) o.O. I will learn how to use vectors when I have the time, as a busy college student and with other things such as bible studies, I barely have enough time! Thank you marvinIsSacul also for all your help. I am now having a problem with my sortList() method for some reason.
2

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

12 Comments

Thank you so much. This really helps. So when I use a C++11 array, I do not need to use delete [] anymore? I will have to use that from now on.
Also, now I am getting an error under my sortList() method. Do you happen to know why?
@Alexandra: If you declare list as a C++11 you do not need to use delete[]. You should think it like a normal variable, which gets automatically deleted when it exits the scope. For the error in sortList() please check first my updated answer.
@francesco I seriousy don't understand why you and others are throwing the kid with the std library. Because I think the kid wants to learn the language first (hence he creates his own algorithms), not the libraries. He'll get to learn them as time goes on.
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.