简体   繁体   English

在 C++ 中通过引用传递结构不会更新值

[英]passing struct by reference in c++ doesnt update the value

i am passing a struct by reference to one function of a class treestr and storing it there in a vector.我通过引用类 treestr 的一个函数传递一个结构并将其存储在一个向量中。

Then passing the same struct by reference to another function and performing some computation on data member of struct.然后通过引用另一个函数传递相同的结构并对结构的数据成员执行一些计算。 This data member gets updated in the original struct but not in the ones stored in the vector.该数据成员在原始结构中得到更新,但不会在向量中存储的结构中得到更新。

(Sorry for any mistakes, new to c++, new to stack overflow). (对于任何错误,C++ 新手,堆栈溢出新手,请见谅)。 Please help.请帮忙。

//Structure description
Struct point{
int x;
int y;
int cal{0};

}; };

Struct node{

    point p;
    int data; //value to be updated by func

};

int main(){

    treestr * tree= new treestr(); //create object
    int i=0,n=100;
    vector<node> nob;
    while(i<=n){
        p={1,5}; //some values input by user
        node n={p,i};
        nob.push_back(n)//storing the struct node objects seperately in a 
                         //vector
        treestr->insert(n);  //inserting into tree class
        i++;
        }
    //calling func to do some computation on the struct objects inserted
    for(i=0;i<n;i++){
        int x=tree->func(nob[i]);
        cout<<x.cal; //getting updated values from the function
        }

    for(i=0;i<N;i++){
        tree->func2(nob[i]);
    }
    return 0;
}

//class description
class treestr{
    vector<node> buck; 
    insert(node& n){
        buck.push_back(n);
        //store nodes
    }

    func(node& n){
        //calculations
        return n.cal; Value got updated in main.
    }
    func2(node &n){
        int val1=n.cal; //this assigns updated value
        int val2=buck[i].p.cal; //this assigns 0(default value)
        if(val1==val2){ //never matches, val2 is 0 for all objects, val1 is 
                           //not after getting updated
        //do something
         }
    }

};

The cal gets updated in the main function but not in the class where I have stored. cal 在 main 函数中更新,但不在我存储的类中。 Please ignore grammatical and syntactical mistakes, the code returns correct output however this is something that I need to improve my code.请忽略语法和句法错误,代码返回正确的输出,但这是我需要改进代码的地方。

Any possible reasons??有什么可能的原因吗??

Try changing node n={1,5,0}尝试更改node n={1,5,0}

to

node * n;
node -> x = 1;
node -> y = 5;
node -> cal = 0;

From what I know of pointers you need to assign each value individually.根据我对指针的了解,您需要单独分配每个值。 Also

tree->insert(n)

needs to be需要是

tree.insert(n)

Another thing另一件事

int node.cal=tree->func(n);

Not sure what this is supposed to do, but I know it won't work.不知道这应该做什么,但我知道它行不通。 'int' needs to be before a variable name. 'int' 需要在变量名之前。 When accessing cal from node your call needs to be an -> and when accessing func from tree it needs to be a .node访问cal ,您的调用必须是->而从tree访问func ,它需要是.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM