Mam problem z poniższym kodem:
#include "stdafx.h"
#include <memory>
#include <vector>
#include <string>
#include <iostream>
class Block;
class Document
{
public:
Document() {}
~Document() {}
private:
std::vector<std::shared_ptr<Block> > item;
public:
void addBlock(std::shared_ptr<Block> block)
{
item.push_back(block);
}
void showBlock(void) const
{
/*for (std::shared_ptr<Block> i : item)
{
i->showText();
}*/
}
};
class Block : public std::enable_shared_from_this<Block>
{
public:
Block(Document& doc, std::string text)
{
doc.addBlock(shared_from_this());
this->text = text;
}
~Block() {}
public:
void showText() const
{
std::cout << text << std::endl;
}
private:
std::string text;
};
int _tmain(int argc, _TCHAR* argv[])
{
Document docObj;
std::shared_ptr<Block> block1(new Block(docObj, "napis1"));
std::shared_ptr<Block> block2(new Block(docObj, "napis2"));
docObj.showBlock();
return 0;
}
Wywala błąd std::bad_weak_ptr at memory location
Za bardzo nie rozumiem, jak powinienem wykorzystać weak_ptr
i jak to powiązać z shared_ptr
.