好吧,我可能做错了,但我已经束手无策了。
我有一个节点类的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我确实更新并尝试在中间添加引用,这就是为什么它们现在在一些地方有引用。我还有同样的问题。
发布于 2018-10-16 14:03:46
我无法与上述意见配合,这主要是我未能提供足够资料的问题。尽管如此,我重新编写了代码,将cruthu::Tera对象作为共享指针传递,并将向量公开为类的公共成员。这是我稍后会再讨论的事情,因为这个实现不是我感到高兴的事情。
代码在github上,不幸的是,这是我的论文工作,所以我不能再等了。
如果人们还想尝试一个答案,我会和他们一起工作。
发布于 2018-10-14 00:02:54
如用户Remy Lebeau所述,请提供一个最小、完整和可验证的示例。
正如您所述,您正在将一个std::vector<std::shared_ptr<Node>>从一个类传递到另一个类,或者从一个函数传递到另一个函数,并且它们没有更新,并且已经初始化为0。从你描述的行为来看,我有一个问题要问你,我把它作为一个回答,因为它太长了,不能发表评论。
接受上述向量或共享指针的函数声明/定义是否如下所示:
void someFunc( std::vector<shared_ptr<Node> nodes ) { ... }或者它看起来像这样:
void someFunc( std::vector<shared_ptr<Node>& nodes ) { ... }我这样问是因为如果您通过值传递容器,而不是通过引用传递容器,情况就会有所不同。
发布于 2018-10-15 07:12:26
这还不是一个答案,而是确定问题的问题,因为没有提供足够的实现。
一个可能的问题(很难说,没有看到执行情况.)是在Run()函数的顶部创建节点:
std::vector<std::shared_ptr<cruthu::Node>> nodes(teraGen->Create());然后在此调用中将该函数作为引用传递:
tera->SetNodes(nodes);tera对节点做了什么?通过引用传递意味着shared_ptr:s的计数不会增加。
this->CreateImage(tera)是做什么的?
Run()完成后是否使用节点?
https://stackoverflow.com/questions/52798181
复制相似问题