【发布时间】:2018-10-13 23:21:25
【问题描述】:
好吧,我可能做错了,但我束手无策。
我有一个节点类的 shared_ptr 向量,我传递给各种东西,我的节点类有一个节点类型的邻居的 share_ptr 向量。
我有一个类为我生成节点网格,并返回一个std::vector<std::shared_ptr<Node>> nodes 和一个重要的std::shared_ptr<Node> significant node。
然后,我将此向量传递给一个索引器,该索引器创建第二个列表,该列表是第一个列表的子集,大小约为 10%,它返回为std::vector<std::shared_ptr<Node>> indexedNodes。
创建这些后,我将它们传递给另一个对象,以供以后参考。 然后修改器类从 indexedNodes 中获取单个随机节点,并使用它遍历节点邻居修改高度值。
稍后,当我将它们导出时,值显示为 0/initialized。
需要注意的一点,我将数据传递给函数并返回 std::vector<std::shared_ptr<Node>> 我认为这是我的问题,我只是不确定如何正确传递我的 shared_ptr 的容器,这样我就不会复制.
如果需要更多信息,请告诉我。我正在寻找我可以理解的示例或参考。
对不起代码,它不漂亮,我使用动态加载的库。
完成工作的函数:
void cruthu::Cruthu::Run() {
std::shared_ptr<cruthu::ITeraGen> teraGen(this->mSettings.TeraGen.Factory->DLGetInstance());
std::vector<std::shared_ptr<cruthu::Node>> nodes(teraGen->Create());
std::shared_ptr<cruthu::Node> significantNode(teraGen->GetSignificantNode());
std::vector<std::shared_ptr<cruthu::IIndexer>> indexers;
for(const auto indexer : this->mSettings.Indexers) {
indexers.push_back(indexer.Factory->DLGetInstance());
}
std::vector<std::shared_ptr<cruthu::Node>> indexedNodes(indexers.at(0)->Index(nodes));
std::shared_ptr<cruthu::ITera> tera(this->mSettings.Tera.Factory->DLGetInstance());
tera->SetNodes(nodes);
tera->SetIndexedNodes(indexedNodes);
tera->SetSignificantNode(significantNode);
for(const auto & formaF : this->mSettings.Formas) {
std::shared_ptr<cruthu::IForma> forma(formaF.Factory->DLGetInstance());
forma->SetNode(tera->GetIndexedNode());
forma->Modify();
std::cout << std::to_string(tera->GetIndexedNode()->GetHeight()) << std::endl;
}
this->CreateImage(tera);
}
TeraGen:
#ifndef CRUTHU_ITERAGEN_HPP
#define CRUTHU_ITERAGEN_HPP
#include <cruthu/Node.hpp>
#include <vector>
namespace cruthu {
class ITeraGen {
public:
virtual ~ITeraGen() = default;
virtual std::vector<std::shared_ptr<cruthu::Node>> Create() = 0;
virtual std::shared_ptr<cruthu::Node> GetSignificantNode() = 0;
};
} // namespace cruthu
#endif
泰拉:
#ifndef CRUTHU_ITERA_HPP
#define CRUTHU_ITERA_HPP
#include <cruthu/IIndexer.hpp>
#include <cruthu/Node.hpp>
#include <memory>
#include <vector>
namespace cruthu {
class ITera {
public:
virtual ~ITera() = default;
virtual void SetNodes(std::vector<std::shared_ptr<cruthu::Node>>& nodes) = 0;
virtual void SetIndexedNodes(std::vector<std::shared_ptr<cruthu::Node>>& indexedNodes) = 0;
virtual void SetSignificantNode(std::shared_ptr<cruthu::Node> significantNode) = 0;
virtual std::vector<std::shared_ptr<cruthu::Node>>& GetNodes() = 0;
virtual std::vector<std::shared_ptr<cruthu::Node>>& GetIndexedNodes() = 0;
virtual std::shared_ptr<cruthu::Node> GetIndexedNode() = 0;
};
} // namespace cruthu
#endif
索引器:
#ifndef CRUTHU_IINDEXER_HPP
#define CRUTHU_IINDEXER_HPP
#include <cruthu/Node.hpp>
#include <memory>
#include <vector>
namespace cruthu {
class IIndexer {
public:
virtual ~IIndexer() = default;
virtual std::vector<std::shared_ptr<cruthu::Node>> Index(std::shared_ptr<cruthu::Node> node) = 0;
virtual std::vector<std::shared_ptr<cruthu::Node>> Index(std::vector<std::shared_ptr<cruthu::Node>>& nodes) = 0;
};
} // namespace cruthu
#endif
形式:
#ifndef CRUTHU_IFORMA_HPP
#define CRUTHU_IFORMA_HPP
#include <cruthu/Node.hpp>
namespace cruthu {
class IForma {
public:
virtual ~IForma() = default;
virtual void SetNode(std::shared_ptr<cruthu::Node> node) = 0;
virtual void Modify() = 0;
};
} // namespace cruthu
#endif
我确实更新并尝试在两者之间添加引用,这就是为什么他们现在在某些地方有引用。我仍然有同样的问题。
【问题讨论】:
-
请提供minimal reproducible example,准确显示您实际在做什么,以及您遇到的问题
-
我的水晶球认为你在某处复制节点。
-
我很快就会得到一个 MCVE。
-
使用 shared_ptrs,除非我用它调用 std::copy,否则我不会重复正确?