M
Malciah
I posted this on another site, but so far I've had no answers.
So, I decided to try it here.
--------------------------------------------------------
I've been learning C++ for about 6 weeks now, and I've been able to
figure out the cause
of pretty much every problem I've run into.
This time, I'm at a loss. The book I'm learning from isn't the
greatest in the world, and it hasn't
given me a good explanation since chapter 6. (I'm in chapter 11
now)
I am learning about template classes, and how to transfer data between
two objects of the same class with pointers involved.
I was shown how to make the code "safe" by adding a copy constructor,
an overloaded assignment operator, and a destructor.
The code only had one object, and I tried to add another object so I
could see if the code worked,
and boom. Problem.
The code compiles with no errors, however when you run it, it gets to
the last 'for' loop and crashes.
Actually, it only crashes if you try to display the values inside the
'newScores' array.
If you remove the last 'cout', it runs with no problem.
Here's the code:
--------------------------------------------------------
#include <iostream>
using namespace std;
template <class T>
class Array
{
T *ptr_to_array;
int number;
public:
Array( int number_of_elements ); // constructor
declaration.
Array( const Array &a ); // copy
constructor declaration.
~Array();
T& operator=( const Array
&a ); // overload the equal
operator.
T& operator[]( int index ){ return ptr_to_array[index]; } //
overload the array index.
};
template <class T>
Array<T>::Array( int number_of_elements ) // constructor
definition.
{
number = number_of_elements;
ptr_to_array = new T[number];
}
template <class T>
Array<T>::Array( const Array &a )
{
number = a.number; // copy
constructor definition.
T *ptr_to_array = new T[number];
for( int index = 0; index < number; index++ )
{
ptr_to_array[index] = a.ptr_to_array[index];
}
}
template <class T>
T& Array<T>:perator=( const Array &a )
{
if ( this == &a ) return *this;
delete[] ptr_to_array;
number = a.number; // overloaded
assignment operator.
T *ptr_to_array = new T[number];
for( int loop = 0; loop < number; loop++ )
{
ptr_to_array[loop] = a.ptr_to_array[loop];
}
return *this;
}
template <class T>
Array<T>::~Array() // Destructor.
{
delete[] ptr_to_array;
}
int main()
{
const int nmbr_kids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Used
to initialize the first object.
Array<float> scores( 10 );
for( int loop = 0; loop < 10; loop++ )
{
scores[loop] = nmbr_kids[loop]; // Initializing first object
array.
}
for( int loop = 0; loop < 10; loop++ )
{ //
Displaying first array.
cout << "This is the value in scores element" << loop << ": " <<
scores[loop] << endl;
}
cout << "\n\n";
// Problem starts below...
Array<float> newScores( scores ); // New object.
// newScores = scores; /* *** See Below *** */
for( int loop = 0; loop < 10; loop++ ) // Display second
array.
{
cout << "This is the value in newScores element" << loop << ": " <<
newScores[loop] << endl;
}
return 0;
}
--------------------------------------------------------
/* *** I ran out of ideas, so I tried adding this line as you see. I
also tried it like this: *
* newScores[10] = scores[10]; & newScores[] = scores[];
They didn't work either *** */
I added the array to the constant integer (originally, it initialized
to 5), and the 'for' loops
in main. (the ones in the method definitions are from the book)
Sorry it's so long...
I'm starting to understand why there's a book dedicated to C++
templates.
If it's not too much to ask, could you also explain why this code
needs the overloaded
assignment operator?
I understand the copy constructor... But the overloaded assignment is
a mystery.
I appreciate any help that anyone can give me on this. It's been
driving me crazy for 4 days
Thank you in advance,
-Malciah
So, I decided to try it here.
--------------------------------------------------------
I've been learning C++ for about 6 weeks now, and I've been able to
figure out the cause
of pretty much every problem I've run into.
This time, I'm at a loss. The book I'm learning from isn't the
greatest in the world, and it hasn't
given me a good explanation since chapter 6. (I'm in chapter 11
now)
I am learning about template classes, and how to transfer data between
two objects of the same class with pointers involved.
I was shown how to make the code "safe" by adding a copy constructor,
an overloaded assignment operator, and a destructor.
The code only had one object, and I tried to add another object so I
could see if the code worked,
and boom. Problem.
The code compiles with no errors, however when you run it, it gets to
the last 'for' loop and crashes.
Actually, it only crashes if you try to display the values inside the
'newScores' array.
If you remove the last 'cout', it runs with no problem.
Here's the code:
--------------------------------------------------------
#include <iostream>
using namespace std;
template <class T>
class Array
{
T *ptr_to_array;
int number;
public:
Array( int number_of_elements ); // constructor
declaration.
Array( const Array &a ); // copy
constructor declaration.
~Array();
T& operator=( const Array
&a ); // overload the equal
operator.
T& operator[]( int index ){ return ptr_to_array[index]; } //
overload the array index.
};
template <class T>
Array<T>::Array( int number_of_elements ) // constructor
definition.
{
number = number_of_elements;
ptr_to_array = new T[number];
}
template <class T>
Array<T>::Array( const Array &a )
{
number = a.number; // copy
constructor definition.
T *ptr_to_array = new T[number];
for( int index = 0; index < number; index++ )
{
ptr_to_array[index] = a.ptr_to_array[index];
}
}
template <class T>
T& Array<T>:perator=( const Array &a )
{
if ( this == &a ) return *this;
delete[] ptr_to_array;
number = a.number; // overloaded
assignment operator.
T *ptr_to_array = new T[number];
for( int loop = 0; loop < number; loop++ )
{
ptr_to_array[loop] = a.ptr_to_array[loop];
}
return *this;
}
template <class T>
Array<T>::~Array() // Destructor.
{
delete[] ptr_to_array;
}
int main()
{
const int nmbr_kids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Used
to initialize the first object.
Array<float> scores( 10 );
for( int loop = 0; loop < 10; loop++ )
{
scores[loop] = nmbr_kids[loop]; // Initializing first object
array.
}
for( int loop = 0; loop < 10; loop++ )
{ //
Displaying first array.
cout << "This is the value in scores element" << loop << ": " <<
scores[loop] << endl;
}
cout << "\n\n";
// Problem starts below...
Array<float> newScores( scores ); // New object.
// newScores = scores; /* *** See Below *** */
for( int loop = 0; loop < 10; loop++ ) // Display second
array.
{
cout << "This is the value in newScores element" << loop << ": " <<
newScores[loop] << endl;
}
return 0;
}
--------------------------------------------------------
/* *** I ran out of ideas, so I tried adding this line as you see. I
also tried it like this: *
* newScores[10] = scores[10]; & newScores[] = scores[];
They didn't work either *** */
I added the array to the constant integer (originally, it initialized
to 5), and the 'for' loops
in main. (the ones in the method definitions are from the book)
Sorry it's so long...
I'm starting to understand why there's a book dedicated to C++
templates.
If it's not too much to ask, could you also explain why this code
needs the overloaded
assignment operator?
I understand the copy constructor... But the overloaded assignment is
a mystery.
I appreciate any help that anyone can give me on this. It's been
driving me crazy for 4 days
Thank you in advance,
-Malciah