I've been scratching my head all day new to operator overloading and my operator+ function computes the data correctly, but I need to pass the data of temp class to operator= to assign it to a separate instance of my class and returning temp does not work (I imagine the data is being destroyed on exit?) the whole idea is from main x = y + z is called to add the data of two vectors from y and z and asign it to x and I get the computation of y + z correctly, but passing it to x I've hit a brick wall what is wrong? or does anyone have idea? This is piece my code from my class
VecXd& operator=(const VecXd& rhs)
{
VecXd<V> temp;
temp.dimension = rhs.dimension;
cout << "operator= check dimension is..." << temp.dimension << endl;
for(int i = 0; i < rhs.dimension; i++) //Passing data to x?
{
cout << "test" << endl;
temp.vecArr[i] = rhs.vecArr[i];
cout << temp.vecArr[i] << " our new value" << endl;
}
}
friend VecXd& operator+(VecXd& lhs, VecXd& rhs){
VecXd<V> temp;
cout << lhs.dimension << "-lhs d-" << rhs.dimension << "-rhs d-" << endl; //works
if(lhs.dimension == rhs.dimension) //dimension level check
{
temp.vecArr = new V[lhs.dimension];
for(int i = 0; i < lhs.dimension; i++)
{
temp.vecArr[i] = lhs.vecArr[i] + rhs.vecArr[i];
cout << temp.vecArr[i] << " our new value" << endl;
}
//return *temp.vecArr;
return temp; //***? how to pass data?
}
else{
cout << "Dimensions do not match!!! Error!" << endl;
}
}
any idea? don't be to harsh... haha..... :l