英文:
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 >
with >
, &
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<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[vector<NODE_MESH *>::size_type(node0)]->ID; //ERROR 1
target->node_pointers[0] = NodeStore[vector<NODE_MESH *>::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<NODE_MESH*> NodeStore;
vector<CELL_MESH*> 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 (&newCell, i, nodes_x*y+x, &NodeStore, &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 <vector>
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<NODE_MESH*> NodeStore;
vector<CELL_MESH*> 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<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[vector<NODE_MESH *>::size_type(node0)]->ID; //ERROR 1
target->node_pointers[0] = NodeStore[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 horizontaly (same y)
for (int x = 0; x < 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, &NodeStore, &CellStore);
i++;
}
}
return 0;
}
答案1
得分: 0
给定一个变量 T* t
,语法 t[x]
等同于 *(t+x)
,这就是引起混淆的原因。具体来说,NodeStore[vector<NODE_MESH *>::size_type(node0)]
的类型是 vector<NODE_MESH *>&
,而不是你期望的 NodeStore 的一个元素。
将你的代码改为通过引用传递变量:
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];
}
}
然后调用如下:
setCellInfo(&newCell, i, nodes_x*y+x, NodeStore, CellStore);
或者,你需要在索引之前解引用指针:
(*NodeStore)[node0]->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<NODE_MESH *>::size_type(node0)]
is of type vector<NODE_MESH *>&
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<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[node0]->ID;
target->node_pointers[0] = NodeStore[node0];
}
}
The call is then simply
setCellInfo (&newCell, i, nodes_x*y+x, NodeStore, CellStore);
Alternatively, you will need to dereference the pointer before indexing:
(*NodeStore)[node0]->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<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];
}
}
With this when using it:
CELL_MESH *newCell = new CELL_MESH;
setCellInfo (newCell, i, nodes_x*y+x, NodeStore, CellStore);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论