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?
c++ class templates inheritance constructor
|
show 9 more comments
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?
c++ class templates inheritance constructor
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.
– Some programmer dude
Nov 15 '18 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.
– Yksisarvinen
Nov 15 '18 at 8:39
1
The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
– Alexandra
Nov 15 '18 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 preferstd::vector
over pointers for this
– UnholySheep
Nov 15 '18 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
– UnholySheep
Nov 15 '18 at 8:52
|
show 9 more comments
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?
c++ class templates inheritance constructor
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?
c++ class templates inheritance constructor
c++ class templates inheritance constructor
edited Nov 15 '18 at 9:30
Alexandra
asked Nov 15 '18 at 8:34
AlexandraAlexandra
369
369
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.
– Some programmer dude
Nov 15 '18 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.
– Yksisarvinen
Nov 15 '18 at 8:39
1
The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
– Alexandra
Nov 15 '18 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 preferstd::vector
over pointers for this
– UnholySheep
Nov 15 '18 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
– UnholySheep
Nov 15 '18 at 8:52
|
show 9 more comments
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.
– Some programmer dude
Nov 15 '18 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.
– Yksisarvinen
Nov 15 '18 at 8:39
1
The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
– Alexandra
Nov 15 '18 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 preferstd::vector
over pointers for this
– UnholySheep
Nov 15 '18 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
– UnholySheep
Nov 15 '18 at 8:52
3
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.
– Some programmer dude
Nov 15 '18 at 8:38
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.
– Some programmer dude
Nov 15 '18 at 8:38
2
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.
– Yksisarvinen
Nov 15 '18 at 8:39
"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.
– Yksisarvinen
Nov 15 '18 at 8:39
1
1
The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
– Alexandra
Nov 15 '18 at 8:45
The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
– Alexandra
Nov 15 '18 at 8:45
1
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– UnholySheep
Nov 15 '18 at 8:51
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– UnholySheep
Nov 15 '18 at 8:51
1
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
– UnholySheep
Nov 15 '18 at 8:52
"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
– UnholySheep
Nov 15 '18 at 8:52
|
show 9 more comments
2 Answers
2
active
oldest
votes
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;
}
1
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
try not usingsortIndicesGainLoss
. try removingsortIndicesGainLoss = list;
and uselist
directly.
– marvinIsSacul
Nov 15 '18 at 9:18
so replace every instance ofsortIndicesGainLoss
withlist
– marvinIsSacul
Nov 15 '18 at 9:19
|
show 2 more comments
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 ofstockListType
In the cpp file remove
sortIndicesGainLoss = list;
, and everywhere use the inherited memberlist
instead ofsortIndicesGainLoss
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.
– Alexandra
Nov 15 '18 at 9:30
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
@Alexandra: If you declarelist
as a C++11 you do not need to usedelete
. You should think it like a normal variable, which gets automatically deleted when it exits the scope. For the error insortList()
please check first my updated answer.
– francesco
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:42
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315269%2fcreating-an-array-of-objects-inheriting-from-a-class-template-and-using-constr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
1
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
try not usingsortIndicesGainLoss
. try removingsortIndicesGainLoss = list;
and uselist
directly.
– marvinIsSacul
Nov 15 '18 at 9:18
so replace every instance ofsortIndicesGainLoss
withlist
– marvinIsSacul
Nov 15 '18 at 9:19
|
show 2 more comments
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;
}
1
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
try not usingsortIndicesGainLoss
. try removingsortIndicesGainLoss = list;
and uselist
directly.
– marvinIsSacul
Nov 15 '18 at 9:18
so replace every instance ofsortIndicesGainLoss
withlist
– marvinIsSacul
Nov 15 '18 at 9:19
|
show 2 more comments
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;
}
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;
}
answered Nov 15 '18 at 9:05
marvinIsSaculmarvinIsSacul
53717
53717
1
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
try not usingsortIndicesGainLoss
. try removingsortIndicesGainLoss = list;
and uselist
directly.
– marvinIsSacul
Nov 15 '18 at 9:18
so replace every instance ofsortIndicesGainLoss
withlist
– marvinIsSacul
Nov 15 '18 at 9:19
|
show 2 more comments
1
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
try not usingsortIndicesGainLoss
. try removingsortIndicesGainLoss = list;
and uselist
directly.
– marvinIsSacul
Nov 15 '18 at 9:18
so replace every instance ofsortIndicesGainLoss
withlist
– marvinIsSacul
Nov 15 '18 at 9:19
1
1
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Thank you, this is why it's not working.
– Alexandra
Nov 15 '18 at 9:10
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
Now I'm getting an error around the point where I do: sortIndicesGainLoss = list;
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
It says: Thread 1: breakpoint 1.1
– Alexandra
Nov 15 '18 at 9:12
try not using
sortIndicesGainLoss
. try removing sortIndicesGainLoss = list;
and use list
directly.– marvinIsSacul
Nov 15 '18 at 9:18
try not using
sortIndicesGainLoss
. try removing sortIndicesGainLoss = list;
and use list
directly.– marvinIsSacul
Nov 15 '18 at 9:18
so replace every instance of
sortIndicesGainLoss
with list
– marvinIsSacul
Nov 15 '18 at 9:19
so replace every instance of
sortIndicesGainLoss
with list
– marvinIsSacul
Nov 15 '18 at 9:19
|
show 2 more comments
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 ofstockListType
In the cpp file remove
sortIndicesGainLoss = list;
, and everywhere use the inherited memberlist
instead ofsortIndicesGainLoss
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.
– Alexandra
Nov 15 '18 at 9:30
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
@Alexandra: If you declarelist
as a C++11 you do not need to usedelete
. You should think it like a normal variable, which gets automatically deleted when it exits the scope. For the error insortList()
please check first my updated answer.
– francesco
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:42
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
|
show 7 more comments
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 ofstockListType
In the cpp file remove
sortIndicesGainLoss = list;
, and everywhere use the inherited memberlist
instead ofsortIndicesGainLoss
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.
– Alexandra
Nov 15 '18 at 9:30
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
@Alexandra: If you declarelist
as a C++11 you do not need to usedelete
. You should think it like a normal variable, which gets automatically deleted when it exits the scope. For the error insortList()
please check first my updated answer.
– francesco
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:42
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
|
show 7 more comments
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 ofstockListType
In the cpp file remove
sortIndicesGainLoss = list;
, and everywhere use the inherited memberlist
instead ofsortIndicesGainLoss
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 ofstockListType
In the cpp file remove
sortIndicesGainLoss = list;
, and everywhere use the inherited memberlist
instead ofsortIndicesGainLoss
edited Nov 15 '18 at 10:38
answered Nov 15 '18 at 9:23
francescofrancesco
9201212
9201212
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.
– Alexandra
Nov 15 '18 at 9:30
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
@Alexandra: If you declarelist
as a C++11 you do not need to usedelete
. You should think it like a normal variable, which gets automatically deleted when it exits the scope. For the error insortList()
please check first my updated answer.
– francesco
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:42
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
|
show 7 more 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.
– Alexandra
Nov 15 '18 at 9:30
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
@Alexandra: If you declarelist
as a C++11 you do not need to usedelete
. You should think it like a normal variable, which gets automatically deleted when it exits the scope. For the error insortList()
please check first my updated answer.
– francesco
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:42
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
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.
– Alexandra
Nov 15 '18 at 9:30
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.
– Alexandra
Nov 15 '18 at 9:30
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
Also, now I am getting an error under my sortList() method. Do you happen to know why?
– Alexandra
Nov 15 '18 at 9:31
@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
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:37
@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
Nov 15 '18 at 9:42
@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
Nov 15 '18 at 9:42
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
@marvinIsSacul I think I have proposed both to "manually" use arrays and a self-defined swap, or to use the std library....
– francesco
Nov 15 '18 at 9:47
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315269%2fcreating-an-array-of-objects-inheriting-from-a-class-template-and-using-constr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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.
– Some programmer dude
Nov 15 '18 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.
– Yksisarvinen
Nov 15 '18 at 8:39
1
The error I get is : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) @Yksisarvinen
– Alexandra
Nov 15 '18 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– UnholySheep
Nov 15 '18 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
– UnholySheep
Nov 15 '18 at 8:52