Derived Template Class Copy Constructor Problem
Sep 28, 2012 at 10:24am UTC
I'm having trouble with my copy constructor is a new derived class I've created. Relevant code is as follows (I can provide other code from my project as well, but I think the error should be contained here):
Derived Class Header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef NUMERICARRAY_HPP
#define NUMERICARRAY_HPP
#include "Array_H.hpp"
namespace CLARK{
namespace Containers{
template <class Type=T> class NumericArray: public Array<Type>
{
private :
public :
NumericArray(); // default constructor
NumericArray(const NumericArray<Type>& nap); // copy constructor
~NumericArray(); // destructor
};
}
}
#ifndef NUMERICARRAY_CPP
#include "NumericArray.cpp"
#endif
#endif
Derived Class Source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#ifndef NUMERICARRAY_CPP
#define NUMERICARRAY_CPP
#include "NumericArray_H.hpp"
namespace CLARK{
namespace Containers{
// Constructors and Destructors:
template <class Type>
NumericArray<Type>::NumericArray() : Array()
{ // default constructor
cout << "Numeric Array constructor call (default)" << endl;
}
template <class Type>
NumericArray<Type>::NumericArray(const NumericArray<Type>& nap) : Array<Type>(nap)
{// copy constructor
cout << "Numeric Array constructor call (copy)" << endl;
}
template <class Type>
NumericArray<Type>::~NumericArray()
{// destructor
cout << "Numeric Array destructor call" << endl;
}
}
}
#endif NUMERICARRAY_CPP
Base Class Header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#ifndef ARRAY_HPP
#define ARRAY_HPP
namespace CLARK{
namespace Containers{
template <class Type=T> class Array
{
private :
Type* m_data; // dynamic array of Type objects
int m_size; // size of array
public :
static int m_default_size; // default size
// Constructors and Destructors:
Array(); // default constructor
Array(const Array<Type>& ap); // copy constructor
~Array(); //destructor
};
}
}
#ifndef ARRAY_CPP
#include "Array.cpp"
#endif
#endif
Base Class Source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#ifndef ARRAY_CPP
#define ARRAY_CPP
#include "Array_H.hpp"
#include "ArrayException_H.hpp"
#include "OutOfBoundsException_H.hpp"
namespace CLARK{
namespace Containers{
// Constructors and Destructors:
template <class Type>
int Array<Type>::m_default_size = 10;
template <class Type>
Array<Type>::Array()
{// Default constructor
m_size = m_default_size; // added this per Peter87's post below
m_data = new Type[m_default_size];
cout << "Array constructor call (default)" << endl;
}
template <class Type>
Array<Type>::Array(const Array<Type>& ap)
{// Copy constructor
m_size = ap.m_size;
m_data = new Type[m_size];
int i;
for (i = 0; i < m_size; i++)
{
m_data[i] = (ap).m_data[i];
}
cout << "Array constructor call (copy)" << endl;
}
template <class Type>
Array<Type>::~Array()
{// Destructor
delete []m_data;
cout << "Array destructor call" << endl;;
}
}
}
#endif ARRAY_CPP
Test Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include "Array_H.hpp"
#include "NumericArray_H.hpp"
#include "ArrayException_H.hpp"
#include "OutOfBoundsException_H.hpp"
using namespace CLARK::Containers;
using namespace CLARK::CAD;
int main()
{
try
{
NumericArray<int > intArray1;
NumericArray<int > intArray2(intArray1);
return 0;
}
catch (ArrayException& err)
{
cout << err.GetMessage() << endl;
}
}
The build completes successfully, but the command window hangs when it gets to the NumericArray copy. It looks like this:
Array constructor call (default)
Numeric Array constructor call (default)
_ [flashing cursor here. Windows never kills it.]
What am I missing here?
Thanks!
Last edited on Sep 28, 2012 at 3:05pm UTC
Sep 28, 2012 at 10:59am UTC
You forgot to initialize m_size in the default Array constructor.
Sep 28, 2012 at 11:10am UTC
Actually, I recently implemented this m_default_size, so I took out the m_size initialization from the Array constructor. Anyway, per your suggestion, I just changed the default Array constructor to this:
1 2 3 4 5 6 7 8 9 10
template <class Type>
int Array<Type>::m_default_size = 10;
template <class Type>
Array<Type>::Array()
{// Default constructor
m_size = m_default_size;
m_data = new Type[m_default_size];
cout << "Array constructor call (default)" << endl;
}
but I'm still receiving the same error. I don't think it's necessary to initialize m_size in the default constructor anymore, is it? Also, it seems to me that the default constructor is working fine. Perhaps I'm missing something else or misunderstanding?
Thanks.
Last edited on Sep 28, 2012 at 11:11am UTC
Sep 28, 2012 at 11:21am UTC
You need to initialize m_size, because it will be used in the copy constructor of intArray2 to set the size correctly.
Sep 28, 2012 at 11:24am UTC
Would the code is my last post be sufficient? Thanks!
Sep 28, 2012 at 11:32am UTC
I also tried simply doing this:
1 2 3 4 5 6 7 8 9 10
template <class Type>
int Array<Type>::m_default_size = 10;
template <class Type>
Array<Type>::Array()
{// Default constructor
m_size = 10;
m_data = new Type[m_default_size];
cout << "Array constructor call (default)" << endl;
}
but that didn't solve the problem either. Thanks.
Last edited on Sep 28, 2012 at 11:33am UTC
Sep 28, 2012 at 12:37pm UTC
Would the code is my last post be sufficient? Thanks!
Yes, it should fix that problem. There could be other problems.
Sep 28, 2012 at 3:06pm UTC
Actually, that did fix it! I forgot to rebuild the solution after that fix. Thanks!!!
Last edited on Sep 28, 2012 at 3:10pm UTC
Topic archived. No new replies allowed.