+= overloading operator

Imagine we want to do something like below with defining operator overloading.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i=0;i<n_element;i++)
{

		 NN[node_0][node_0]  = NN[node_0][node_0]  + NN_e[0][0];
		 NN[node_0][node_1]  = NN[node_0][node_1]  + NN_e[0][1];
		 NN[node_0][node_2]  = NN[node_0][node_2]  + NN_e[0][2];
		 NN[node_1][node_0]  = NN[node_1][node_0]  + NN_e[1][0];
		 NN[node_1][node_1]  = NN[node_1][node_1]  + NN_e[1][1];
		 NN[node_1][node_2]  = NN[node_1][node_2]  + NN_e[1][2];
		 NN[node_2][node_0]  = NN[node_2][node_0]  + NN_e[2][0];
		 NN[node_2][node_1]  = NN[node_2][node_1]  + NN_e[2][1];
		 NN[node_2][node_2]  = NN[node_2][node_2]  + NN_e[2][2];

}


and node_0,node_1,and node_2 are coming from the connectivity matrix which is n_element*4 and
1
2
3
4
5
6
7
for (int i=0;i<n_element;i++)
{

		int node_0=((connectivity[i].Node_0 - 1));
		int node_1=((connectivity[i].Node_1 - 1));
		int node_2=((connectivity[i].Node_2 - 1));
}

I've already defined the summation operator (matrix+ scalar) and summation operator (matrix+matrix) but in does not work properly in this case and I'm not sure how to define the proper overloading operator for this. I would be grateful if you could help me with this.
Last edited on
Please show the declarations of NN and NN_e. Also show the declarations of their types and the overloaded operations.

You will probably want to overload += operators so you can say:
NN[node_0][node_0] += NN_e[0][0]
etc.
Yes,I want to do exactly what you said.
Someone can help with this please.
I've already said what you need to show us so that we can help you. How can we help you define overloaded operators when you haven't shown the classes involved?
@dhayden I'm attaching the class for the operation which does the summation between a matrix and a scalar. In this case every element of this matrix is being added with a scalar and it is not a problem.

for the present case the problem is we need to do something like this NN[node_0][node_0] += NN_e[0][0] and at the end the NN matrix will be for example 100 by 100 but my NN_e matrix will be 3 by 3 all the time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class T>
Matrix2D<T> operator+ (const Matrix2D<T>& lhs, const T& rhs)
{
	int numRows = lhs.m_nRows;
	int numCols = lhs.m_nCols;
	int numElements = numRows * numCols;
	T *tempResult = new T[numElements];
	for (int i = 0; i < numElements; ++i)
		tempResult[i] = lhs.m_matrixData[i] + rhs;

	Matrix2D<T> result(numRows, numCols, tempResult);
	delete[] tempResult;
	return result;
}


I can not use also the operator for matrix + matrix because they are not in the same dimension

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class T>
Matrix2D<T> operator+ (const Matrix2D<T>& lhs, const Matrix2D<T>& rhs)
{
	int numRows = lhs.m_nRows;
	int numCols = lhs.m_nCols;
	int numElements = numRows * numCols;
	T *tempResult = new T[numElements];
	for (int i = 0; i < numElements; i++)
		tempResult[i] = lhs.m_matrixData[i] + rhs.m_matrixData[i];

	Matrix2D<T> result(numRows, numCols, tempResult);
	delete[] tempResult;
	return result;
} 


so for now I don't know how I can define a operator which can do this for me.
Last edited on
I wish you would provide all the information I requested.

It appears that you can't use an overloaded operator because the operation you're trying to do is pretty non-standard. At the very least, it has 5 arguments instead of 2: NN, NN_e, node_0, node_1, and node_2. Perhaps you should create a member function to do this:
const Matrix2D<T> &Matrix2D<T>::func(const Matrix2D<T> &rhs, int node_0, int node_1, int node_2);

Anytime you find yourself writing variables ending in _0, _1, _2 etc., you should consider whether they should be an array or vector instead. So maybe it should be:
const Matrix2D<T> &Matrix2D<T>::func(const Matrix2D<T> &rhs, int nodes[3]);
@dhayden can you explain more how I can define this? I still have problem with this, what do you mean by func?
Perhaps if you send me the class declaration like I've been asking all along, then I can help you define the function.
@dhayden
thanks
Last edited on
*
Last edited on
OT: What is the practical difference of NodeID<std::string> and ElemID<std::string>?


What values do you expect to get from:
1
2
3
4
5
6
double calgrad( Point p0, Point p1, Point p2 )
{
  double gx1, gy1, gx2;
  // code
  return gx1, gy1, gx2;
}

See https://en.cppreference.com/w/cpp/language/operator_other
Last edited on
NodeID and ElemID, they are different value,one is the number of node and the other is the number of element.
About the second part I expect to get the gx1,gy1,gx2,gy2,gx3 and gy3, it was my mistake,I missed some of them in the return value.
1
2
int x;
int y;

The 'x' and 'y' will have different values. How can they both have type int?
Of course they can. One can use same type.

However, the main point is that NodeID and ElemID are templates. Why are they templates? What does the template parameter do?


If you do read the description of operator, you will notice that it gives only one double value.

Besides, the function double calgrad( Point, Point, Point ); returns exactly one double, not six. All non-void functions return one value.
Thanks for your answer, but still I don't get how I can do the assembly,could you help me with that?
What do you mean by "do the assembly"? Are you trying to return 6 numbers? Then return a vector<double> that contains them.
Topic archived. No new replies allowed.