gpt4 book ai didi

c++ - 映射中的c++/元素通过退出函数神秘地消失

转载 作者:行者123 更新时间:2023-12-01 14:52:40 25 4
gpt4 key购买 nike

我的 map 代码有问题,将尽我所能解释。

我的代码:(我简化了代码,只保留导致问题的部分)

[prog.cpp]


#include <vector>
#include <iostream>
#include <memory>
#include <fstream>

//this hpp files includes all my other hpp files, i checked the dependencies, all is ok with this
#include "dolmen.hpp"

int main(int argc, char const *argv[]) {
//creating a dolmen element
dolmen::Dolmen DolMen;

//reading the datas
std::ifstream trame("trame.txt");
std::ofstream ofs{"report.csv"};
std::string dataTxt;

//std::map<int, dolmen::Sensor*> sensorList = dolmen::initialise();
//std::map<int, dolmen::Sensor*> sensorList = std::map<int, dolmen::Sensor*>();


//creating a factory element
/*using AFactory = dolmen::FactorySensor<std::string, std::unique_ptr<dolmen::Sensor>, int, std::string>;
AFactory factory;*/

//creating a map to store all the sensors
std::map<int, std::unique_ptr<dolmen::Sensor>> sensorList;
//std::map<int, dolmen::Sensor*> sensorList = std::map<int, dolmen::Sensor*>();
dolmen::initialise(sensorList);

//i'm gonna simplify the call of the method of my sensor
[grabbing a sensor in my sensor x list]
x->getID();
//my x doesn't exists


[sensorInit.hpp]

#ifndef DOLMEN_SENSOR_INIT_HPP
#define DOLMEN_SENSOR_INIT_HPP 1

/*The DolMen user will only need to modify this file if he needs to add new sensors*/

#include "factorySensor.hpp"
#include "sensor.hpp"
//insert here your sensors hpp files
#include "temperature.hpp"
#include "gyroscope.hpp"
#include "acceleration.hpp"
#include "altitude.hpp"
#include "gps.hpp"
#include "pressure.hpp"

namespace dolmen
{
//using AFactory = FactorySensor<std::strin //finding the correct sensor in the sensor list by matching the id with the data frame id
//dolmen::Sensor* elem;
std::unique_ptr<dolmen::Sensor> elem;
if (auto it = sensorList.find(id); it != sensorList.end())
{
elem = it->second;
}

//searching the maximum number of datas returned by the sensor
//(some sensors can return one value, some can return many values)
int max = 0;
if (elem->getID() == id)
{
//decoding the data with the correct sensor, to create a map which contain all the datas of the sensor, from our data frame
elem->decoding(data); //finding the correct sensor in the sensor list by matching the id with the data frame id
//dolmen::Sensor* elem;
std::unique_ptr<dolmen::Sensor> elem;
if (auto it = sensorList.find(id); it != sensorList.end())
{
elem = it->second;
}

//searching the maximum number of datas returned by the sensor
//(some sensors can return one value, some can return many values)
int max = 0;
if (elem->getID() == id)
{
//decoding the data with the correct sensor, to create a map which contain all the datas of the sensor, from our data frame
elem->decoding(data);

if(abs(int(elem->getValue().size())/elem->getNbAttr()) > max)
{
max = int(elem->getValue().size());
}
}
else
{
std::cout << "\nsensor not found\n";
}

if(abs(int(elem->getValue().size())/elem->getNbAttr()) > max)
{
max = int(elem->getValue().size());
}
}
else
{
std::cout << "\nsensor not found\n";
}g, std::unique_ptr<Sensor>, int, std::string>;

//this file is used to create all the new sensors, the user will only need to modify this one
inline void initialise(std::map<int, std::unique_ptr<dolmen::Sensor>>& sensorList)
{
//creating a factory element
using AFactory = FactorySensor<std::string, std::unique_ptr<Sensor>, int, std::string>;
AFactory factory;

/*//creating a map to store all the sensors
std::map<int, Sensor*> sensorList = std::map<int, Sensor*>();*/

//---to change to a for---
//this is a pointer to a sensor element, used to move the sensors from the factory to our map
Sensor* sensor = nullptr;

//the ksp project uses: 00 time / 01 temperature / 02 pressure / 03 acceleration/ 04 gps/ 05 altitude/ 06 gyroscope

//i only create one sensor for the tests, but i can create many
//creating a temperature sensor
factory.registe("temp_sensor", [](int arg1, std::string arg2) { return std::make_unique<Temperature>(arg1,arg2); });
std::unique_ptr<Sensor> temperature = factory.create("temp_sensor", 01, "temp");
sensor = temperature.get();
sensorList.insert(std::make_pair(sensor->getID(), std::move(temperature)));

//this part works perfectly, i can see all my sensors, call the methods, no problem
for (const auto &elem: sensorList)
{
std::cout << "depuis sensorinit------je suis une id de sensor " << elem.second->getID() << "\n";
std::cout << "depuis sensorinit------je suis un nom de sensor " << elem.second->getName() << "\n";
}
}

} /* dolmen */

#endif


所以问题是:只要我在 initialise()中,我的传感器就存在于我的 map 中,但是一旦我退出此功能,它们就会突然消失。
我尝试了在 initialise()内部直接在 prog.cpp中拥有的代码,一切顺利,因此问题仅在我尝试对代码进行非本地化时出现。

我的理论是:在退出函数时,unique_ptr消失了,但是我真的不知道这是不是,如果是这个,我该如何解决?

祝你今天愉快

PS:抱歉,英语不是我的母语

编辑:现在问题出在别的地方,在一个名为dolmen.cpp的文件中,在一个名为解码的方法中(dolmen是一个类)

多尔门

std::string Dolmen::decoding(std::string data, std::map<int, std::unique_ptr<dolmen::Sensor>> sensorList)
{
//finding the correct sensor in the sensor list by matching the id with the data frame id
//dolmen::Sensor* elem; --> this is my old version, this worked until my sensor list became to use unique_ptr instead of *
std::unique_ptr<dolmen::Sensor> elem;
if (auto it = sensorList.find(id); it != sensorList.end())
{
elem = it->second; //the problem is here
}

//searching the maximum number of datas returned by the sensor
//(some sensors can return one value, some can return many values)
int max = 0;
if (elem->getID() == id)
{
//decoding the data with the correct sensor, to create a map which contain all the datas of the sensor, from our data frame
elem->decoding(data);

if(abs(int(elem->getValue().size())/elem->getNbAttr()) > max)
{
max = int(elem->getValue().size());
}
}
else
{
std::cout << "\nsensor not found\n";
}
}

最好的情况如下:我创建我的sensorList一次,然后对于要解码的每一行数据,我调用Dolmen::decoding(因此unique_ptr会引起一些问题...)

惠普可以解决这个问题吗?

最佳答案

您需要将unique_ptr保留在 map 中,这是您需要使其工作的简化版本:

#include <iostream>
#include <memory>
#include <map>

void initialise(std::map<int, std::unique_ptr<int>>& map) {
for(auto i = 0; i < 10; ++i) {
auto newObject = std::make_unique<int>(i);
map[i] = std::move(newObject);
}
}

int main() {
std::map<int, std::unique_ptr<int>> map;
initialise(map);

for(const auto& e : map) {
std::cout << *(e.second) << std::endl;
}

return 0;
}

你可以看到它工作 here

关于c++ - 映射中的c++/元素通过退出函数神秘地消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61931144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com