With the following code i need to store new object in pointer vector. I've done it but now it wont show any information about object in loop (where I go trough vector). In class Tvrtka (company) I have pointer vector of Zaposlenik(worker). For every object I store it goes to vector.
class Tvrtka{
public:
string oib;
string naziv;
string adresa;
vector<const Zaposlenik*> zaposlenici;
Tvrtka(){}
Tvrtka(const string _oib, const string _naziv,const string _adresa, vector<const Zaposlenik*> _z)
{
this->oib=_oib;
this->naziv=_naziv;
this->adresa=_adresa;
for(int i=0;_z.size();i++)
{
zaposlenici.push_back(_z[i]);
}
}
Main part:
vector<const Zaposlenik*>zaposlenici;
Zaposlenik *z1=new Zaposlenik("Marko", "maric", 20);
zaposlenici.push_back(z1);
Tvrtka t1("198","Majur", "2222", zaposlenici);
for(int i=0;i<zaposlenici.size();i++)
{
cout<<zaposlenici[i]->ime;
}
If I comment t1(object of Tvrtka) it shows me information about worker.
In this loop
for(int i=0;_z.size();i++)
{
zaposlenici.push_back(_z[i]);
}
_z.size()
is used as loop condition and this is not dependent with i
. This means that if you pass non-empty vector as _z
, the loop will go infinitely and it will run out of memory.
It should be
for(int i=0;i<_z.size();i++)
{
zaposlenici.push_back(_z[i]);
}
or simply
for(auto& elem : _z)
{
zaposlenici.push_back(elem);
}
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.