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?
}
输出是某种布什
如何正确地为这种类型的矢量分配内存,这些类型的矢量由智能指针组成,将自定义的类作为类型?
==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)
你如何声明你的'Portfel'对象?你在'Inwestor'构造函数中做什么? – 2013-05-07 11:08:30
在Inwestor :: Inwestor我做 Portfel p1; – user1960582 2013-05-07 11:16:27
当Inwestor :: nowy_profil()调用operator <<时,你粘贴的错误发生。请显示该方法。 – Useless 2013-05-07 11:35:41