在以下情況下,sortMyVectors
的參數列表如何顯示,以及如何從myFunction
內部調用它?將矢量傳遞給函數(指針/地址)
std::vector<Vector2> myFunction (MyObj * myObj) {
std::vector<Vector2> myVectors;
// fill with lots of Vector2(double x, double y) objects
sortMyVectors(???-myVectors, ???-myObj);
return myVectors;
}
void sortMyVectors(vector<Vector2> &myVectors, const MyObj &myObj) {
// sort the std::vector using myObj
// modifies order of Vector2s within the passed-in myVectors
// does not modify myObj
// need to be able to access with myVectors[0].x here...
}
這就是所謂的方法是myFunction(&(*myObj));
,其中myObj
是list<MyObj>::iterator
。有沒有更清晰的方式來寫這個?
沒什麼特別的:'sortMyVectors(myVectors,* myObj);''myVectors'是一個變量,所以你可以直接將它傳遞給一個引用; 'myObj'是一個指針,所以你需要解除引用它才能與'MyObj&'兼容。 – dasblinkenlight 2012-02-29 18:34:40
你會得到什麼錯誤? – dasblinkenlight 2012-02-29 18:46:05
Thx爲解釋。但是,我得到這個錯誤:「未定義引用sortMyVectors(std :: vector <_Vector2,std :: allocator <_Vector2>>&,MyObj&)'」 – Ben 2012-02-29 18:48:27