REFERENCE DELIVERY OF STRUCTURE USING VECTOR

Asked 1 years ago, Updated 1 years ago, 32 views

What should I do if I want to change the value of a vector structure in another class?
Below is the current state of the code.

Structure

classst{
    structure hoge {
        int score1;
        int score2;
        intsum;
    }
}

class data {
public:
    vector<TestScore>*Data::GetData(){
    return&_studentScore;
    }
}

class cal {
public:
    int sumCal(vector<hoge>*sScore){
        sScore[i]->sum=sScore[i]->score1+sScore[i]->score2;
    }
}

If you try to access the core1,2 of the structure with the sumCal function of the above code with the Arrow operator,
A warning will be issued that a pointer type is required for the ceremony.
->* will fail if core1 does not exist.

c++

2022-09-30 21:25

1 Answers

The question title says "reference", but the source code provided is not a reference.There seems to be some confusion about the relationship between reference, pointer, and substance.

The suggested source uses the pointer to vector in the function sumCal, but I guess this is probably wrong and what I really want to do is refer to vector.Then the function declaration is

 int sumCal (vector<hoge>&sScore)

It must be .And vector<hoge> stores the value of hoge itself, so to access score1 and so on in a function, it should be . instead of ->.Results

int sumCal(vector<hoge>&sScore){
    sScore[i].sum=sScore[i].score1+sScore[i].score2;
}

I guessed that might be the code you wanted to write.

There are a lot of things like where i came from and there is no return value for sumCal, but I think that's going to get off the point, so I have to go through it.


2022-09-30 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.