Search code examples
c++pointersconstructorprogram-entry-point

passing pointers through multiple functions from main() C++


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".


Solution

  • 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();
    }