Получение номера мусора в производном классе шаблона

Я пытаюсь вычислить точечный продукт в производном классе шаблона, но он продолжает возвращать номер мусора

Пример: экспоненциальное число.

Я полагаю, что это может быть связано с конструктором в производном классе? Я вызвал конструктор копирования базового класса из производного класса, но не уверен, стоит ли инициализировать закрытые члены базового класса. Заранее спасибо!

template <typename T>//declared as a template of type T

NumericArray<T>::NumericArray(): Array<T>() {       //constructor
    cout <<"The NumArray constructor has been called." << endl;
}

template <typename T>//declared as a template of type T

NumericArray<T>::NumericArray(int new_size): Array<T>(new_size){    
    //parameter constructor
    cout << "The NumArray parameter constructor has been called." << endl;
}

template <typename T>

NumericArray<T>::NumericArray(const NumericArray<T>& arr) : Array<T>
(arr) { //copy constructor
    cout << "The NumArray copy constructor has been called." << endl;
}

template <typename T>       //declared as a template of type T

NumericArray<T>::~NumericArray() {              //destructor
    cout << "The destructor of NumArray." << endl;
}

template <typename T>//declared as a template of type T

double NumericArray<T>::DotProduct(const NumericArray<T>& a)    const           
//dot product
{

    if ((*this).size() != a.size())
        throw NumArrayException();//throw exception if the sizes are not the same

        double dotProduct = 0;//initialize dot product

        for (int i = 0; i < (*this).size(); i++)    //iterates the elements
        {
            dotProduct += (a.getElement(i) )* (NumericArray::getElement(i));        
        //calculate dot product
        }

        return dotProduct;

}

и в базовом классе Array...

template <typename T>

Array<T>::Array(int size)
{
    m_size = size; //set the size to be the input size
    m_data = new T[size]; //set the dynamic array to be of different size
    //cout << "size constructor" << endl;
}

template <typename T>

Array<T>::Array(const Array<T>& arr)
{
    m_size = arr.m_size;        //copy to be same size
    m_data = new T[m_size]; //copy dynamic memory of new size

    for (int i = 0; i < arr.m_size; i++) //loop through each element of new array
    {
        m_data[i] = arr.m_data[i];  //copy each new array element
    }

    cout << "copy constructor is used." << endl;
}

0 ответов

Другие вопросы по тегам