向量 - push_back使用默认构造函数不复制构造函数 [英] Vector - push_back uses default constructor not copy constructor

查看:115
本文介绍了向量 - push_back使用默认构造函数不复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着向我的向量中添加新元素的问题。
让我们从herarchy开始



索引公司。公司包含库存

 索引
{
...
vector< Company&组成;
...
}

公司
{
...
向量< Stock>股票;
...
}

库存
{
....
}



问题是当我开始添加一个公司股票(直到这一刻一切正常)到一个索引

  Comapny c1(bla bla,some argument); 
mindex-> composition.push_back(c1);

其中mindex定义为

  Index * mindex = new Index(); 

我有一些内存错误,我决定把一些 cout<



$ b $

b

  mindex-> composition.push_back(c1); 

正在执行(首先我将其大小设为 mindex-> .resize(0); )它的大小被放大,以便其中的一切都被移动到临时向量和组合一切都被删除。但是向量不使用复制构造函数,而是使用默认构造函数创建Stocks。

 库存::库存()
{
cout<<默认构造函数\\\
;
}

我在屏幕上显示了约6次。



问题是:为什么vector在扩大时不使用copy contructor?
(我有我的复制构造函数声明和定义)



COMPLETE CODE WHERE THING HAPPENS:
对不起我的语言 - 它是波兰语: / p>

  Akcja =库存
Spolka =公司
Indeks =索引

void Portfel< ; T,T2,T3> :: dodaj_spolke()
{
std :: string nazwa;
double cena_emisji
double aktualna_cena;
usi ilosc_akcji;
std :: string indeks;
cout<<Podaj nazwe spolki:\\\
; // UNIMPORTANT
cin>> nazwa;
cout<<Podaj cene emisji akcji przy IPO。\\\
;
cin>> cena_emisji;
cout<<Ile akcji jest w obiegu?\\\
;
cin>> ilosc_akcji;
cout<<Jaka jest aktualna cena?\\\
;
cin>> aktualna_cena;
if(mindex-> GetNast()== NULL)
{
cout<<Spolka zostanie przypisana domyslnie do WIG。\\\
;
indeks =WIG;
}
else
{
cout<<<Do jakiego indeksu nalezy spolka?\\\
;
cin>> indeks;
} // TILL THIS MOMENT
Spolka s1(nazwa,cena_emisji,aktualna_cena,ilosc_akcji,indeks);
cout<<----------------------------------------- --\\\
;
for(int i = 0; i <(int)s1.akcje.size(); i ++)
{
cout<Spolka:< s1.akcje [i] .spolka<< endl;
cout<<Cena aktualna:<< s1.akcje [i] .cena_aktualna<<< endl;
cout<<Twoja cena zakupu:<<< s1.akcje [i] .cena_zakupu<<<
}
cout<<------------------------------------ ------- \\\
;
cout<<tutaj po dodaniu spolki \\\
;
cout<<Przed zmiana \\\
;
Spolka s2 = s1;
mindex-> sklad.push_back(Spolka s1); // tutajbłąd
cout<<Zmiana rozmiaru wektora - tutaj pomiedzy \\\
;
Mindex-> ilosc_spolek ++;
for(int i = 0; i {
mindex-> sklad [mindex-> ilosc_spolek-1] .akcje [i] .spolka = nazwa;
mindex-> sklad [mindex-> ilosc_spolek-1] .akcje [i] .cena_aktualna = aktualna_cena;
Akcja :: Akcja()
{
// std :: cout<<< sizeof(this)<< std :: endl;
cout<<Domyslny konstruktor \\\
;
}
Akcja :: Akcja(std :: string nazwa,double akt)
{
spolka = nazwa;
cena_zakupu = 0;
cena_aktualna = akt;
zysk = cena_zakupu-cena_aktualna;
}
Akcja :: Akcja(const Akcja& a1)
{
this-> spolka = a1.spolka;
this-> cena_zakupu = a1.cena_zakupu;
this-> cena_aktualna = a1.cena_aktualna;
this-> zysk = a1.zysk;
} mindex-> sklad [mindex-> ilosc_spolek-1] .akcje [i] .cena_zakupu = 0;
mindex-> sklad [mindex-> ilosc_spolek-1] .akcje [i] .zysk = 0;

}
cout<<tutaj koniec\\\
;
cout<<Ilosc spolek w mindex:<< mindex-> sklad.size()<<\\\
;
cout<< mindex-> ilosc_spolek<< endl;
cout<<----------------------------------------- --\\\
;
for(int i = 0; i< mindex-> ilosc_spolek; i ++)
{
cout<< mindex-> sklad [i] .GetNazwa()< endl;
cout<< mindex-> sklad [i] .akcje [0] .GetSpolka()<<<
cout<< mindex-> sklad [i] .akcje [0] .cena_zakupu<<< endl;
cout<< mindex-> sklad [i] .akcje [0] .cena_aktualna<<< endl;
}
cout<<------------------------------------ ------- \\\
;
getchar();
}

spolka.cpp(仅涉及部分 - 无需粘贴其余部分的代码,但我显然可以,如果你愿意)

  Spolka :: Spolka(string naz,double cena_e,double cena_a, usi ilosc,string ind)
{
nazwa = naz;
cena_emisji = cena_e;
aktualna_cena = cena_a;
ilosc_akcji = ilosc;
akcje.resize(0);
for(int i = 0; i {
akcje.push_back(Akcja(naz,cena_e));
}
indeks = ind;
std :: cout<<Dodano spolke:<< this-> nazwa<<<
}

akcja.cpp

  Akcja :: Akcja()
{
// std :: cout<<< sizeof(this)<< std :: endl;
cout<<Domyslny konstruktor \\\
;
}
Akcja :: Akcja(std :: string nazwa,double akt)
{
spolka = nazwa;
cena_zakupu = 0;
cena_aktualna = akt;
zysk = cena_zakupu-cena_aktualna;
}
Akcja :: Akcja(const Akcja& a1)
{
this-> spolka = a1.spolka;
this-> cena_zakupu = a1.cena_zakupu
this--> cena_aktualna = a1.cena_aktualna;
this-> zysk = a1.zysk;
}

以及效果:



添加第一家公司之后

  tutaj po dodaniu spolki 
Przed zmiana
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Zmiana rozmiaru wektora - tutaj pomiedzy
tutaj koniec
Ilosc spolek w mindex:1
1
-------------------------------------- -----
KGHM
KGHM
0
160
--------------------- ----------------------
Usunieto:KGHM
Usunieto:KGHM

并且添加第二个公司

tutaj po dodaniu spolki
Przed zmiana
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Domyslny konstruktor
Usunieto:KGHM
Zmiana rozmiaru wektora - tutaj pomiedzy
tutaj koniec
Ilosc spolek w mindex:2
2
------------------------------------- ------
KGHM

== 2375 ==条件跳转或移动取决于未初始化的值
== 2375 == at 0x539A683:__printf_fp(printf_fp .c:406)
== 2375 == by 0x53975B7:vfprintf(vfprintf.c:1629)
== 2375 == by 0x53BF441:vsnprintf(vsnprintf.c:120)
= = 2375 == by 0x4EB62DF:??? (在/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
== 2375 == by 0x4EBC715:std :: ostreambuf_iterator< char,std :: char_traits< char> >

我知道很多,但我真的非常感谢您的帮助。

解决方案

  mindmindex-> composition.resize(0); 

这真的应该是 0 吗?假设你调整了向量的大小, resize 是调用默认构造函数,所以这是你看到的原因默认构造函数 6次。你可能想要的是 reserve ,而不是 resize ,它不调用默认构造函数。


I'm facing the problem of adding new elements to my vector. Let's start with the herarchy

Index conatins Companies. Companies contain Stocks

Index
{
...
vector <Company> composition;
...
}

Company
{
...
vector <Stock> stocks;
...
}

Stock
{
....
}

The problem is when I start adding a company with stocks (till this moment everything is ok) to an index

Comapny c1(bla bla, some arguments);
mindex->composition.push_back(c1);

Where "mindex" is defined as

 Index* mindex=new Index();

I got some memory errors and I decided to put some cout<<"here\n"; in some parts of the program to see what was going on.

While

mindex->composition.push_back(c1);

is being executed, (at first I sized it mindex->composition.resize(0); ) it's size is being enlarged so that everything in it is moved to the temporary vector and in composition everything is deleted. But then vector uses not copy constructor, but default constructor to create the Stocks.

Stock::Stock()
{
  cout<<"The default constructor\n";
}

I got this on the screen about 6 times.

The question is: Why vector doesn't use copy contructor when it is being enlarged? (I have my copy constructor declared and defined)

COMPLETE CODE WHERE THE THING HAPPENS: Sorry for my language - it's Polish :)

Akcja=Stock
Spolka=Company
Indeks=Index

void Portfel<T,T2,T3>::dodaj_spolke()
{
  std::string nazwa;
  double cena_emisji;
  double aktualna_cena;
  usi ilosc_akcji;   
  std::string indeks;
  cout<<"Podaj nazwe spolki: \n";                      //UNIMPORTANT
  cin>>nazwa;
  cout<<"Podaj cene emisji akcji przy IPO. \n";
  cin>>cena_emisji;
  cout<<"Ile akcji jest w obiegu ?\n";
  cin>>ilosc_akcji;
  cout<<"Jaka jest aktualna cena? \n";
  cin>>aktualna_cena;
  if (mindex->GetNast()==NULL)
  {
      cout<<"Spolka zostanie przypisana domyslnie do WIG. \n";
      indeks="WIG";
  }
  else
  {
    cout<<"Do jakiego indeksu nalezy spolka? \n";
    cin>>indeks;
  }                                                         //TILL THIS MOMENT
Spolka s1(nazwa,cena_emisji,aktualna_cena,ilosc_akcji,indeks);
cout<<"-------------------------------------------\n";
for (int i=0;i<(int)s1.akcje.size();i++)
{
 cout<<"Spolka: "<<s1.akcje[i].spolka<<endl;
  cout<<"Cena aktualna: "<<s1.akcje[i].cena_aktualna<<endl;
  cout<<"Twoja cena zakupu: "<<s1.akcje[i].cena_zakupu<<endl;
} 
cout<<"-------------------------------------------\n";
cout<<"tutaj po dodaniu spolki \n";
cout<<"Przed zmiana \n"; 
Spolka s2=s1;
mindex->sklad.push_back(Spolka s1); // tutaj błąd
cout<<"Zmiana rozmiaru wektora - tutaj pomiedzy \n";
mindex->ilosc_spolek++;
for (int i=0;i<ilosc_akcji;i++)
{
mindex->sklad[mindex->ilosc_spolek-1].akcje[i].spolka=nazwa;
mindex->sklad[mindex->ilosc_spolek-1].akcje[i].cena_aktualna=aktualna_cena;
Akcja::Akcja()
{
  //std::cout<<sizeof(this)<<std::endl;
  cout<<"Domyslny konstruktor \n";
}
Akcja::Akcja(std::string nazwa,double akt)
{
  spolka=nazwa;
  cena_zakupu=0;
  cena_aktualna=akt;
  zysk=cena_zakupu-cena_aktualna;
}
Akcja::Akcja(const Akcja& a1)
{
  this->spolka=a1.spolka;
  this->cena_zakupu=a1.cena_zakupu;
  this->cena_aktualna=a1.cena_aktualna;
  this->zysk=a1.zysk;
}    mindex->sklad[mindex->ilosc_spolek-1].akcje[i].cena_zakupu=0;
mindex->sklad[mindex->ilosc_spolek-1].akcje[i].zysk=0;

}
cout<<"tutaj koniec\n";
cout<<"Ilosc spolek w mindex: "<<mindex->sklad.size()<<"\n";
cout<<mindex->ilosc_spolek<<endl;
cout<<"-------------------------------------------\n";
for (int i=0;i<mindex->ilosc_spolek;i++)
{
    cout<<mindex->sklad[i].GetNazwa()<<endl;
    cout<<mindex->sklad[i].akcje[0].GetSpolka()<<endl;
    cout<<mindex->sklad[i].akcje[0].cena_zakupu<<endl;
    cout<<mindex->sklad[i].akcje[0].cena_aktualna<<endl;
}
cout<<"-------------------------------------------\n";
getchar();
}

spolka.cpp (the concerning part only - there's no need to paste the rest of the code, but I obviously may if you wish)

Spolka::Spolka(string naz,double cena_e,double cena_a,usi ilosc,string ind)
{
  nazwa=naz;
  cena_emisji=cena_e;
  aktualna_cena=cena_a;
  ilosc_akcji=ilosc;
  akcje.resize(0);
   for(int i=0;i<ilosc;i++)
   {
    akcje.push_back(Akcja (naz,cena_e));
   }
  indeks=ind;
  std::cout<<"Dodano spolke: "<<this->nazwa<<endl;
}

akcja.cpp

Akcja::Akcja()
{
  //std::cout<<sizeof(this)<<std::endl;
  cout<<"Domyslny konstruktor \n";
}
Akcja::Akcja(std::string nazwa,double akt)
{
  spolka=nazwa;
  cena_zakupu=0;
  cena_aktualna=akt;
  zysk=cena_zakupu-cena_aktualna;
}
Akcja::Akcja(const Akcja& a1)
{
  this->spolka=a1.spolka;
  this->cena_zakupu=a1.cena_zakupu;
  this->cena_aktualna=a1.cena_aktualna;
  this->zysk=a1.zysk;
}

and the effects:

after adding first company

    tutaj po dodaniu spolki 
    Przed zmiana 
    Domyslny konstruktor 
    Domyslny konstruktor 
    Domyslny konstruktor 
    Domyslny konstruktor 
    Domyslny konstruktor 
    Domyslny konstruktor 
    Zmiana rozmiaru wektora - tutaj pomiedzy 
    tutaj koniec
    Ilosc spolek w mindex: 1
    1
    -------------------------------------------
    KGHM
    KGHM
    0
    160
    -------------------------------------------
    Usunieto: KGHM
    Usunieto: KGHM

and after adding second company

tutaj po dodaniu spolki 
Przed zmiana 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Domyslny konstruktor 
Usunieto: KGHM
Zmiana rozmiaru wektora - tutaj pomiedzy 
tutaj koniec
Ilosc spolek w mindex: 2
2
-------------------------------------------
KGHM

==2375== Conditional jump or move depends on uninitialised value(s)
==2375==    at 0x539A683: __printf_fp (printf_fp.c:406)
==2375==    by 0x53975B7: vfprintf (vfprintf.c:1629)
==2375==    by 0x53BF441: vsnprintf (vsnprintf.c:120)
==2375==    by 0x4EB62DF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==2375==    by 0x4EBC715: std::ostreambuf_iterator<char, std::char_traits<char> >    

I know it's much but I would be really grateful for help.

解决方案

mindmindex->composition.resize(0);

Was that really supposed to be 0? Assuming you resized the vector, resize is what calls the default constructor, so that is the reason why you see The default constructor 6 times. What you probably wanted is reserve and not resize which does not call the default constructor.

这篇关于向量 - push_back使用默认构造函数不复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆