C++ 测试重载 + 运算符
C++ testing overloaded + operator
我只是将两个大小为 3 的数值数组相加并打印结果。因此,如果:
A = 4,6,1
B = 8,5,6
那么结果就是12,11,7。我的问题是我正在打印指数数字,即 -1.28823e-231。谁能告诉我为什么?我试图在这里保留尽可能少的代码。首先是 main(),然后是 header,然后是源代码 material。非常感谢。
NumericArray<double> doubArray5; //test + operator
cout << "doubArray5" << endl;
doubArray5 = doubArray2 + doubArray3;
for (int i = 0; i < doubArray5.Size(); i++)
cout << doubArray5[i] << endl;
cout << endl;
#ifndef NUMERICARRAY_H
#define NUMERICARRAY_H
#include "array.h"
#include <iostream>
namespace Cary
{
namespace Containers
{
template<typename T>
class NumericArray: public Array<T>
{
public:
NumericArray<T>(); //default constructor
NumericArray<T>(int i); //constructor with one argument
~NumericArray<T>(); //destructor
NumericArray<T>(const NumericArray<T>& source); //copy constructor
NumericArray<T>& operator = (const NumericArray<T>& arr1); //assignment operator
NumericArray<T> operator * (double factor) const; //scale
NumericArray<T>& operator + (const NumericArray<T>& arr2) const; //add
T dotProduct(const NumericArray<T>& na) const; //dot product
};
}
}
#ifndef NUMERICARRAY_CPP
#include "NumericArray.cpp"
#endif
#endif
template<typename T>
NumericArray<T>& NumericArray<T>::operator + (const NumericArray<T>& arr) const //add
{
if (Array<T>::Size() != arr.Size()) throw OutOfBoundsException();
NumericArray<T> tempArray = NumericArray(Array<T>::Size());
for (int i = 0; i < Array<T>::Size(); i++)
tempArray[i] = Array<T>::GetElement(i) + arr.GetElement(i);
return tempArray;
}
惯用地(即基于重载数字运算符时的 "behaves like int" 指南),operator+()
通常按值 returns,而不是按引用,因为加法的结果是一个不同的值(或对象)来自被添加的任何一个。
具体来说,正如 Mike Seymour 在评论中也提到的那样,您的 operator+()
正在返回对在 operator+()
returns 时不复存在的局部变量的引用。如果调用者随后尝试使用返回的引用,这会导致调用者表现出未定义的行为。
您正在返回对局部变量(operator +
中的tempArray
)的引用。
函数returns时,tempArray
被销毁。然后调用者尝试使用对现在已销毁对象的引用,并读取垃圾。
我只是将两个大小为 3 的数值数组相加并打印结果。因此,如果: A = 4,6,1 B = 8,5,6 那么结果就是12,11,7。我的问题是我正在打印指数数字,即 -1.28823e-231。谁能告诉我为什么?我试图在这里保留尽可能少的代码。首先是 main(),然后是 header,然后是源代码 material。非常感谢。
NumericArray<double> doubArray5; //test + operator
cout << "doubArray5" << endl;
doubArray5 = doubArray2 + doubArray3;
for (int i = 0; i < doubArray5.Size(); i++)
cout << doubArray5[i] << endl;
cout << endl;
#ifndef NUMERICARRAY_H
#define NUMERICARRAY_H
#include "array.h"
#include <iostream>
namespace Cary
{
namespace Containers
{
template<typename T>
class NumericArray: public Array<T>
{
public:
NumericArray<T>(); //default constructor
NumericArray<T>(int i); //constructor with one argument
~NumericArray<T>(); //destructor
NumericArray<T>(const NumericArray<T>& source); //copy constructor
NumericArray<T>& operator = (const NumericArray<T>& arr1); //assignment operator
NumericArray<T> operator * (double factor) const; //scale
NumericArray<T>& operator + (const NumericArray<T>& arr2) const; //add
T dotProduct(const NumericArray<T>& na) const; //dot product
};
}
}
#ifndef NUMERICARRAY_CPP
#include "NumericArray.cpp"
#endif
#endif
template<typename T>
NumericArray<T>& NumericArray<T>::operator + (const NumericArray<T>& arr) const //add
{
if (Array<T>::Size() != arr.Size()) throw OutOfBoundsException();
NumericArray<T> tempArray = NumericArray(Array<T>::Size());
for (int i = 0; i < Array<T>::Size(); i++)
tempArray[i] = Array<T>::GetElement(i) + arr.GetElement(i);
return tempArray;
}
惯用地(即基于重载数字运算符时的 "behaves like int" 指南),operator+()
通常按值 returns,而不是按引用,因为加法的结果是一个不同的值(或对象)来自被添加的任何一个。
具体来说,正如 Mike Seymour 在评论中也提到的那样,您的 operator+()
正在返回对在 operator+()
returns 时不复存在的局部变量的引用。如果调用者随后尝试使用返回的引用,这会导致调用者表现出未定义的行为。
您正在返回对局部变量(operator +
中的tempArray
)的引用。
函数returns时,tempArray
被销毁。然后调用者尝试使用对现在已销毁对象的引用,并读取垃圾。