[I am converting some array using college exercises to use vectors, but I need some help.]
I need to know how to use a vector in calling a function originally written using an array. If someone could show me some examples (my prototypes seem okay, but my calls are conflicted: "cannot convert parameter 1 from 'studentType [20]' to 'std::vector<_Ty>'"
1 2 3 4 5 6 7 8 9 10
/*Prototype*/void fnReadData(std::ifstream&, std::vector<studentType>, int);
/*Call*/fnReadData(ReadFile, students, 20);
/*Definition*/void fnReadData(ifstream &inFile, std::vector<studentType> sTy, int nArraySize) {
while (inFile) {
for (int i = 0; i < nArraySize; i++)
inFile >> sTy[i].stStudentFName >> sTy[i].stStudentLName >> sTy[i].nTestScore;
}
}
If you want to modify the vector in the function, you need to pass it in as an reference or a pointer (reference makes more sense in this case). Also you need to make sure that the vector actually has the amount of elements that you need - you can do this by either creating the vector with as many elements as you need, use the resize function or change your function to create a vector, push_back the elements on it and return it.