Click here to Skip to main content
15,034,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Imagine we want to do some matrix assembly with defining operator overloading. node_0,node_1,and node_2 are coming from the connectivity matrix which is n_element*4.
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.

What I have tried:

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));

		 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];

}
Posted
Updated 6-Jul-21 11:15am
v4
Comments
jeron1 6-Jul-21 11:56am
   
Does this even compile? The variables (node_0, node_1, node_2) created in the first for loop are out of scope by the time they are used in the second for loop.
Richard MacCutchan 6-Jul-21 12:21pm
   
Maybe if you showed us the matrix code you already have, it would help us to understand the rest. What you have posted above gives very little useful information
Stefan_Lang 7-Jul-21 5:19am
   
If you could implement an operator for adding a scalar, then I don't get what is your problem? You should show the code you have for operator+({scalar type}), and the code you tried for operator+=({element type}). Then we have a chance to find and point out your problem.

Posting the code that you want to use that operator in doesn't tell us anything.

1 solution

That code is ... pretty poor. Not only will it not compile as Jeron1 has said, even if it did it couldn't work as the first loop is irrelevant: the values in node_n will always be the last elements in the connectivity array since you overwrite the value each time round the loop.

The same thing then happens with the second loop!

Forget trying to define operators; get your code right first or you are just wasting your time!
   
Comments
OriginalGriff 6-Jul-21 12:30pm
   
So if that's just "a piece of code" what have you tried to actually do the work?
jeron1 6-Jul-21 12:31pm
   
Update the original post with any changes.
Richard MacCutchan 6-Jul-21 15:32pm
   
Use copy and paste so you get the exact same text as on your system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900