传递带有结构的向量指针 C++

huangapple go评论94阅读模式
英文:

Passing pointers of vectors with structures C++

问题

I've translated the code you provided as requested:

  1. void setCellInfo(CELL_MESH* target, int Global_ID, int node0, vector<NODE_MESH *>* NodeStore, vector<CELL_MESH *>* CellStore) {
  2. CellStore->push_back(target); // No Errors
  3. target->Global_ID = Global_ID; // No Errors
  4. if (node0 != 0) {
  5. target->node[0] = (*NodeStore)[static_cast<vector<NODE_MESH *>::size_type>(node0)]->ID; // ERROR 1
  6. target->node_pointers[0] = (*NodeStore)[static_cast<vector<NODE_MESH *>::size_type>(node0)]; // ERROR 2
  7. }
  8. }
  9. int main() {
  10. int i = 0;
  11. for (double y = 0; y < nodes_y; y++) {
  12. for (double x = 0; x < nodes_x; x++) {
  13. NODE_MESH* newNode = new NODE_MESH;
  14. NodeStore.push_back(newNode);
  15. newNode->ID = i;
  16. i++;
  17. }
  18. }
  19. i = 0;
  20. for (int y = 0; y < cells_y; y++) { // nodes_y since horizontal faces are aligned with nodes horizontally (same y)
  21. for (int x = 0; x < cells_x; x++) { // x coordinate for horizontal faces is in-between nodes, so 0.5 with a count for faces
  22. CELL_MESH* newCell = new CELL_MESH;
  23. setCellInfo(newCell, i, nodes_x * y + x, &NodeStore, &CellStore);
  24. i++;
  25. }
  26. }
  27. return 0;
  28. }

I've made the necessary translations, such as replacing &gt; with >, &amp; with &, and using static_cast to cast the index to the appropriate type.

英文:

I've declared the following function in C++

  1. void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;* NodeStore, vector&lt;CELL_MESH *&gt;* CellStore) {
  2. CellStore-&gt;push_back(target); //No Errors
  3. target-&gt;Global_ID = Global_ID; //No Errors
  4. if (node0 != 0) {
  5. target-&gt;node[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]-&gt;ID; //ERROR 1
  6. target-&gt;node_pointers[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]; //ERROR 2
  7. }
  8. }

ERROR1: Gives me a "No member named 'ID' in 'std::vector<NODE_MESH *>'" for the target->node[] attributions although its the entities from the pointers within the vector that have this ID member. Since I'm trying to get a specific entity in the vector using NodeStore[value], I would think it would work.

ERROR2: Gives me "Assigning to 'NODE_MESH *' from incompatible type 'vector<NODE_MESH *>'" for all the target->node_pointers attributions. This seems to be the same problem but with pointers directly (without the ID member).

the NodeStore and CellStore vectors a defined as follows outside the function

  1. vector&lt;NODE_MESH*&gt; NodeStore;
  2. vector&lt;CELL_MESH*&gt; CellStore;

I then try to use the function like this, with 'i' being the int Global_ID and 'nodes_x*y+x' being some integer.

  1. CELL_MESH *newCell = new CELL_MESH;
  2. setCellInfo (&amp;newCell, i, nodes_x*y+x, &amp;NodeStore, &amp;CellStore);

I've tried many different alterations to pointers but can't get it work. Would you know how to ?

Here's a simplified complete version:

  1. #include &lt;vector&gt;
  2. using namespace std;
  3. typedef struct NODE_MESH{
  4. int ID;
  5. }NODE_MESH;
  6. typedef struct CELL_MESH{
  7. int Global_ID;
  8. NODE_MESH* node_pointers[4];
  9. int node[4];
  10. }CELL_MESH;
  11. vector&lt;NODE_MESH*&gt; NodeStore;
  12. vector&lt;CELL_MESH*&gt; CellStore;
  13. double nodes_y = 5;
  14. double nodes_x = 4;
  15. int cells_y = 4;
  16. int cells_x = 3;
  17. void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;* NodeStore, vector&lt;CELL_MESH *&gt;* CellStore) {
  18. CellStore-&gt;push_back(target); //No Errors
  19. target-&gt;Global_ID = Global_ID; //No Errors
  20. if (node0 != 0) {
  21. target-&gt;node[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]-&gt;ID; //ERROR 1
  22. target-&gt;node_pointers[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]; //ERROR 2
  23. }
  24. }
  25. int main() {
  26. int i = 0;
  27. for (double y = 0; y &lt; nodes_y; y++) {
  28. for (double x = 0; x &lt; nodes_x; x++) {
  29. NODE_MESH *newNode = new NODE_MESH;
  30. NodeStore.push_back(newNode);
  31. newNode -&gt; ID = i;
  32. i++;
  33. }
  34. }
  35. i = 0;
  36. for (int y = 0; y &lt; cells_y; y++) { //nodes_y since horizontal faces are aligned with nodes horizontaly (same y)
  37. for (int x = 0; x &lt; cells_x; x++) { //x coordinate for horizontal faces is in-between nodes so 0.5 with count for faces
  38. CELL_MESH *newCell = new CELL_MESH;
  39. setCellInfo (newCell, i, nodes_x*y+x, &amp;NodeStore, &amp;CellStore);
  40. i++;
  41. }
  42. }
  43. return 0;
  44. }

答案1

得分: 0

给定一个变量 T* t,语法 t[x] 等同于 *(t+x),这就是引起混淆的原因。具体来说,NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)] 的类型是 vector&lt;NODE_MESH *&gt;&amp;,而不是你期望的 NodeStore 的一个元素。

将你的代码改为通过引用传递变量:

  1. void setCellInfo (CELL_MESH* target, int Global_ID, int node0, vector&lt;NODE_MESH *&gt;&amp; NodeStore, vector&lt;CELL_MESH *&gt;&amp; CellStore) {
  2. CellStore-&gt;push_back(target); //没有错误
  3. target-&gt;Global_ID = Global_ID; //没有错误
  4. if (node0 != 0) {
  5. target-&gt;node[0] = NodeStore[node0]-&gt;ID;
  6. target-&gt;node_pointers[0] = NodeStore[node0];
  7. }
  8. }

然后调用如下:

  1. setCellInfo(&amp;newCell, i, nodes_x*y+x, NodeStore, CellStore);

或者,你需要在索引之前解引用指针:

  1. (*NodeStore)[node0]-&gt;ID
英文:

Given a variable T* t, the syntax t[x] is equivalent to *(t+x), which is the cause of this confusion. Concretely, NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)] is of type vector&lt;NODE_MESH *&gt;&amp; instead of an element of the NodeStore as you expected.

Change your code to take variables by reference instead:

  1. void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;&amp; NodeStore, vector&lt;CELL_MESH *&gt;&amp; CellStore) {
  2. CellStore-&gt;push_back(target); //No Errors
  3. target-&gt;Global_ID = Global_ID; //No Errors
  4. if (node0 != 0) {
  5. target-&gt;node[0] = NodeStore[node0]-&gt;ID;
  6. target-&gt;node_pointers[0] = NodeStore[node0];
  7. }
  8. }

The call is then simply

  1. setCellInfo (&amp;newCell, i, nodes_x*y+x, NodeStore, CellStore);

Alternatively, you will need to dereference the pointer before indexing:

  1. (*NodeStore)[node0]-&gt;ID

答案2

得分: 0

最终解决方案:

  1. void setCellInfo(CELL_MESH* target, int Global_ID, int node0, vector<NODE_MESH *>& NodeStore, vector<CELL_MESH *>& CellStore) {
  2. CellStore.push_back(target);
  3. target->Global_ID = Global_ID;
  4. if (node0 != 0) {
  5. target->node[0] = NodeStore[node0]->ID;
  6. target->node_pointers[0] = NodeStore[node0];
  7. }
  8. }

在使用时:

  1. CELL_MESH *newCell = new CELL_MESH;
  2. setCellInfo(newCell, i, nodes_x * y + x, NodeStore, CellStore);
英文:

Final Solution:

  1. void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;&amp; NodeStore, vector&lt;CELL_MESH *&gt;&amp; CellStore) {
  2. CellStore.push_back(target);
  3. target-&gt;Global_ID = Global_ID;
  4. if (node0 != 0) {
  5. target-&gt;node[0] = NodeStore[node0]-&gt;ID;
  6. target-&gt;node_pointers[0] = NodeStore[node0];
  7. }
  8. }

With this when using it:

  1. CELL_MESH *newCell = new CELL_MESH;
  2. setCellInfo (newCell, i, nodes_x*y+x, NodeStore, CellStore);

huangapple
  • 本文由 发表于 2023年2月6日 19:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定