c++/ pointers/ constructor/ main

this isn't quite working out. Let's see if we can collectively expand our knowledge on this one. Okay:

vector<vector<Point>> aVectorOfPoints
int main(){
    someConstructor(&aVectorOfPoints)
}

someConstructor(vector<vector<Point>>* aVectorOfPoints){
    functionOne(aVectorOfPOints);
}

functionOne(vector<vector<Point>>* aVectorOfPOints){
    aVectorOfPoints[i][j] = getPointFromClass();
}
//functionX(...){...}

I'm getting some error underneath the assignment in functionOne. How can I better do this? Thanks.

The specific error is "No operator '=' matches these operands".

Use references instead of pointers:

someConstructor( vector<vector<Point>> &aVectorOfPoints) {

and the same for functionOne .

Your mistake is that aVectorOfPoints[i] indexes the pointer by i . If using pointers you'd need to dereference the pointer first before doing that, by writing (*aVectorOfPoints)[i][j] .

Why is this wrong?

aVectorOfPoints[i][j] = getPointFromClass();

type of aVectorOfPoints is vector<vector<Point>>* .
type of aVectorOfPoints[i] is vector<vector<Point>> .
type of aVectorOfPoints[i][j] is vector<Point> .

A Point cannot be assigned to a vector<Point> . Hence the compiler error.

Perhaps you meant to use:

(*aVectorOfPoints)[i][j] = getPointFromClass();

You can simplify the code by passing references.

int main(){
    someConstructor(aVectorOfPoints)
}

someConstructor(vector<vector<Point>>& aVectorOfPoints){
    functionOne(aVectorOfPOints);
}

functionOne(vector<vector<Point>>& aVectorOfPOints){
    aVectorOfPoints[i][j] = getPointFromClass();
}

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question C++ Passing pointers through class functions from main() c++ passing nullptr through multiple functions passing struct pointers to functions c++ Passing Function Pointers to Functions, c++ C++ passing pointers Returning pointers or references from functions C++ C++ functions and pointers Pointers and Functions in C++ C++ functions with pointers pointers to functions in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM