我在函数类中使用类的向量的push_back时遇到问题。
我的代码是[所有变量都是葡萄牙语,对不起]:
我的主:
int main(int argc, char* argv[])
{
//blah blah before here...
vector <C_Aerogerador*> ptrAerogeradores;
//the following lines are just for example
int size = 4;
for (int i = 0; i < size; i++)
{
ptrAerogeradores.push_back(new C_Aerogerador);
}
//OK, now I have a vector of pointer with size 4
//Creating an object of C_Aerogerador to call a function Class of C_Aerogerador
C_Aerogerador * teste = new C_Aerogerador;
teste->carregaConfig(ptrAerogeradores);
//PROBLEM! After the calling of the function carregaConfig the
// size of ptrAerogeradores is still 4
}
C_Aerogerador´Class的功能carregaConfig为:
void C_Aerogerador::carregaConfig(vector <C_Aerogerador*> ptrAerogerador)
{
//blah blah, setting some values in ptrAerogerador
// example:
ptrAerogerador[0]->areaVarrida = 5;
//after adding some values I want to push back the ptrAerogerador
ptrAerogerador.push_back(new C_Aerogerador);
// It didn't work.
}
最佳答案
更改您的函数签名以引用此vector
void C_Aerogerador::carregaConfig(vector <C_Aerogerador*>& ptrAerogerador);
// ^ Note
否则,您只是在function参数堆栈上更改原始
vector
的副本。