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

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

Passing pointers of vectors with structures C++

问题

I've translated the code you provided as requested:

void setCellInfo(CELL_MESH* target, int Global_ID, int node0, vector<NODE_MESH *>* NodeStore, vector<CELL_MESH *>* CellStore) {

    CellStore->push_back(target); // No Errors
    target->Global_ID = Global_ID; // No Errors

    if (node0 != 0) {
        target->node[0] = (*NodeStore)[static_cast<vector<NODE_MESH *>::size_type>(node0)]->ID; // ERROR 1
        target->node_pointers[0] = (*NodeStore)[static_cast<vector<NODE_MESH *>::size_type>(node0)]; // ERROR 2
    }
}

int main() {

    int i = 0;

    for (double y = 0; y < nodes_y; y++) {
        for (double x = 0; x < nodes_x; x++) {
            NODE_MESH* newNode = new NODE_MESH;
            NodeStore.push_back(newNode);
            newNode->ID = i;
            i++;
        }
    }

    i = 0;

    for (int y = 0; y < cells_y; y++) { // nodes_y since horizontal faces are aligned with nodes horizontally (same y)
        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

            CELL_MESH* newCell = new CELL_MESH;

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

            i++;
        }
    }

    return 0;
}

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++

void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;* NodeStore, vector&lt;CELL_MESH *&gt;* CellStore) {

    CellStore-&gt;push_back(target); //No Errors
    target-&gt;Global_ID = Global_ID; //No Errors
    
    if (node0 != 0) {
        target-&gt;node[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]-&gt;ID; //ERROR 1
        target-&gt;node_pointers[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]; //ERROR 2
    }
}

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

vector&lt;NODE_MESH*&gt; NodeStore;
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.

CELL_MESH *newCell = new CELL_MESH;

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:

#include &lt;vector&gt;

using namespace std;

typedef struct NODE_MESH{
    int ID;
}NODE_MESH;

typedef struct CELL_MESH{
    int Global_ID;
    NODE_MESH* node_pointers[4];
    int node[4];
}CELL_MESH;

vector&lt;NODE_MESH*&gt; NodeStore;
vector&lt;CELL_MESH*&gt; CellStore;

double nodes_y = 5;
double nodes_x = 4;
int cells_y = 4;
int cells_x = 3;

void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;* NodeStore, vector&lt;CELL_MESH *&gt;* CellStore) {

    CellStore-&gt;push_back(target); //No Errors
    target-&gt;Global_ID = Global_ID; //No Errors
    
    if (node0 != 0) {
        target-&gt;node[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]-&gt;ID; //ERROR 1
        target-&gt;node_pointers[0] = NodeStore[vector&lt;NODE_MESH *&gt;::size_type(node0)]; //ERROR 2
    }
}

int main() {
    
    int i = 0;
    
    for (double y = 0; y &lt; nodes_y; y++) {
        for (double x = 0; x &lt; nodes_x; x++) {
            NODE_MESH *newNode = new NODE_MESH;
            NodeStore.push_back(newNode);
            newNode -&gt; ID = i;
            i++;
        }
    }
    
    i = 0;
    
    for (int y = 0; y &lt; cells_y; y++) { //nodes_y since horizontal faces are aligned with nodes horizontaly (same y)
        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
            
            CELL_MESH *newCell = new CELL_MESH;
            
            setCellInfo (newCell, i, nodes_x*y+x, &amp;NodeStore, &amp;CellStore);
            
            i++;
            
        }
    }
    
    return 0;
}

答案1

得分: 0

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

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

void setCellInfo (CELL_MESH* target, int Global_ID, int node0, vector&lt;NODE_MESH *&gt;&amp; NodeStore, vector&lt;CELL_MESH *&gt;&amp; CellStore) {

    CellStore-&gt;push_back(target); //没有错误
    target-&gt;Global_ID = Global_ID; //没有错误
    
    if (node0 != 0) {
        target-&gt;node[0] = NodeStore[node0]-&gt;ID;
        target-&gt;node_pointers[0] = NodeStore[node0];
    }
}

然后调用如下:

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

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

(*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:

void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;&amp; NodeStore, vector&lt;CELL_MESH *&gt;&amp; CellStore) {

    CellStore-&gt;push_back(target); //No Errors
    target-&gt;Global_ID = Global_ID; //No Errors
    
    if (node0 != 0) {
        target-&gt;node[0] = NodeStore[node0]-&gt;ID;
        target-&gt;node_pointers[0] = NodeStore[node0];
    }
}

The call is then simply

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

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

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

答案2

得分: 0

最终解决方案:

void setCellInfo(CELL_MESH* target, int Global_ID, int node0, vector<NODE_MESH *>& NodeStore, vector<CELL_MESH *>& CellStore) {

    CellStore.push_back(target);
    target->Global_ID = Global_ID;

    if (node0 != 0) {
        target->node[0] = NodeStore[node0]->ID;
        target->node_pointers[0] = NodeStore[node0];
    }
}

在使用时:

CELL_MESH *newCell = new CELL_MESH;

setCellInfo(newCell, i, nodes_x * y + x, NodeStore, CellStore);
英文:

Final Solution:

void setCellInfo (CELL_MESH* target, int Global_ID, int node0,vector&lt;NODE_MESH *&gt;&amp; NodeStore, vector&lt;CELL_MESH *&gt;&amp; CellStore) {

    CellStore.push_back(target);
    target-&gt;Global_ID = Global_ID;
    
    if (node0 != 0) {
        target-&gt;node[0] = NodeStore[node0]-&gt;ID;
        target-&gt;node_pointers[0] = NodeStore[node0];
    }
}

With this when using it:

CELL_MESH *newCell = new CELL_MESH;
        
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:

确定