2013-05-07 91 views
1

我有關於向量分配向量,它的內存類型是自己的類C++

分配內存(Allocating memory for class in C++ Allocating memory for class in C++,例如)至今幾帖 但我沒有設法找到了解決方案我現在面臨的問題。

讓我們去。據S.Prata,

tab.push_back(); 

分配爲我們把所謂的「標籤」 如果是這樣的載體的新對象的內存,我們沒有聲明長度聲明中的矢量,對嗎?

vecotr<Type> tab; 

所以,知道這些事實,我想和你們分享。

instrumenty.h

#pragma once 
#include "portfel.h" 
#include <string> 
#include <memory> 

class Spolka; 

class Akcja 
{ 
    std::shared_ptr<Spolka> firma; 
    double cena_zakupu; 
    double cena_aktualna; 
public: 
    Akcja(); 
    ~Akcja(); 
}; 

class Obligacja 
{ 
    int a; 
public: 
    Obligacja(); 
    ~Obligacja(); 
}; 

class Kontrakt 
{ 
    int a; 
public: 
    Kontrakt(); 
    ~Kontrakt(); 
}; 

現在去 「portfel.h」

#pragma once 
#include <fstream> 
#include <vector> 
#include <memory> 
#include "instrumenty.h" 

class Akcja; 
class Obligacja; 
class Kontrakt; 

class Portfel 
{ 
    friend class Inwestor; 
    int a; 
    std::vector< std::unique_ptr<Akcja> > akcje; 
    std::vector< std::unique_ptr<Obligacja> > obligacje; 
    std::vector< std::unique_ptr<Kontrakt> > kontrakty; 
public: 
    Portfel(); 
    ~Portfel(); 
    friend std::ofstream& operator<<(std::ofstream& zapisz, Portfel& p1); 
}; 

portfel.cpp(問題的原因)

Portfel::Portfel() 
{ 
    a=0; 
    akcje.push_back(NULL);  // <------- THIS THING 
    //akcje.reserve(class Akcja); // <-- How to properly define this according to 
             portfel.h? 
} 

輸出是某種布什

enter image description here

如何正確地爲這種類型的矢量分配內存,這些類型的矢量由智能指針組成,將自定義的類作爲類型?

==3238== Invalid read of size 2 
==3238== at 0x409336: operator<<(std::basic_ofstream<char, std::char_traits<char> >&, Inwestor*) (zapis.cpp:13) 
==3238== by 0x4066B5: Inwestor::nowy_profil() (inwestor.cpp:116) 
==3238== by 0x404B00: menu1() (funkcje.cpp:97) 
==3238== by 0x40208E: main (main.cpp:27) 
==3238== Address 0x5a07140 is 8 bytes after a block of size 8 alloc'd 
==3238== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==3238== by 0x408B13: __gnu_cxx::new_allocator<std::unique_ptr<Akcja, std::default_delete<Akcja> > >::allocate(unsigned long, void const*) (new_allocator.h:94) 
==3238== by 0x40892A: std::_Vector_base<std::unique_ptr<Akcja, std::default_delete<Akcja> >, std::allocator<std::unique_ptr<Akcja, std::default_delete<Akcja> > > >::_M_allocate(unsigned long) (in /home/rideofyourlife/Pulpit/DM/all) 
==3238== by 0x4084C3: void std::vector<std::unique_ptr<Akcja, std::default_delete<Akcja> >, std::allocator<std::unique_ptr<Akcja, std::default_delete<Akcja> > > >::_M_emplace_back_aux<std::unique_ptr<Akcja, std::default_delete<Akcja> > >(std::unique_ptr<Akcja, std::default_delete<Akcja> >&&) (vector.tcc:405) 
==3238== by 0x408234: void std::vector<std::unique_ptr<Akcja, std::default_delete<Akcja> >, std::allocator<std::unique_ptr<Akcja, std::default_delete<Akcja> > > >::emplace_back<std::unique_ptr<Akcja, std::default_delete<Akcja> > >(std::unique_ptr<Akcja, std::default_delete<Akcja> >&&) (vector.tcc:102) 
==3238== by 0x407E5D: std::vector<std::unique_ptr<Akcja, std::default_delete<Akcja> >, std::allocator<std::unique_ptr<Akcja, std::default_delete<Akcja> > > >::push_back(std::unique_ptr<Akcja, std::default_delete<Akcja> >&&) (stl_vector.h:900) 
==3238== by 0x407AE2: Portfel::Portfel() (portfel.cpp:6) 
==3238== by 0x405D07: Inwestor::Inwestor() (inwestor.cpp:13) 
==3238== by 0x402126: __static_initialization_and_destruction_0(int, int) (main.cpp:14) 
==3238== by 0x402161: _GLOBAL__sub_I_q (main.cpp:31) 
==3238== by 0x40985C: __libc_csu_init (in /home/rideofyourlife/Pulpit/DM/all) 
==3238== by 0x536D6FF: (below main) (libc-start.c:185) 
+0

你如何聲明你的'Portfel'對象?你在'Inwestor'構造函數中做什麼? – 2013-05-07 11:08:30

+0

在Inwestor :: Inwestor我做 Portfel p1; – user1960582 2013-05-07 11:16:27

+0

當Inwestor :: nowy_profil()調用operator <<時,你粘貼的錯誤發生。請顯示該方法。 – Useless 2013-05-07 11:35:41

回答

0

要指向一個新Akcja添加到載體中,你會說

akcje.push_back(new Akcja); 

您通常不需要在所有管理vector記憶 - 這是類是什麼。

特別是,reserve用於確保在您事先知道需要多少時確保所有元素都有空間,因此您可以節省重新分配和複製矢量到新位置所需的時間,因爲它增長,或避免內存碎片(這是非常罕見的需要擔心)。
它不用於添加元素。