gpt4 book ai didi

c++ - 在映射中插入一对时,C++ 是否需要额外的代码?

转载 作者:行者123 更新时间:2023-12-01 14:36:43 24 4
gpt4 key购买 nike

伟大的 StackOverflow 社区,您好!我正在制作一些 C++,但在 std::map 中插入元素时遇到问题.

这里有 2 张 map ,存储了一个类似 ID 的 unsigned int作为键和另一个对象作为值:

    std::map<unsigned int, FIFO> _fifos;
std::map<unsigned int, Kitchen> _kitchens;

两张 map 都是private在类里面,我像这样插入 public该类的方法:

    FIFO newFIFO(_internalCount);
Kitchen newKitchen(_args, newFIFO);

_kitchens.insert(std::make_pair(_internalCount, newKitchen));
_fifos.insert(std::make_pair(_internalCount, newFIFO));

麻烦从这里开始。
我的编辑器 (VSCode) 和编译器 (g++) 似乎都接受 _fifos.insert()但不是 _kitchens.insert() .

VSCode 告诉我们:

no instance of overloaded function "std::map<_Key, _Tp, _Compare, _Alloc>::insert [with _Key=unsigned int, _Tp=Kitchen, _Compare=std::less<unsigned int>, _Alloc=std::allocator<std::pair<const unsigned int, Kitchen>>]" matches the argument list..."

虽然 g++ 首先显示这个,但在列出 C++ 深处的一堆错误之后(特别是在 STL_pair.h 中):

error: no matching function for call to ‘std::pair<unsigned int, Kitchen>::pair(unsigned int&, Kitchen&)’
66 | _kitchens.insert(std::pair<unsigned int, Kitchen>(_internalCount, newKitchen));
| ^


我已经尝试过其他 std::pair定义,如 this another question但没有成功。
考虑到我对 C++ 深度的了解不足,这里是否存在类型/语法问题或 std::pair 是否存在任何“缺失”的问题? ?
在此先感谢您的支持!


编辑:
感谢您的建议和代码示例/复制文章的链接。
要添加一点上下文,这是一个学生项目,旨在学习和生成并发代码。
主要想法是创建一个比萨店,其中一个对象 Reception代表主要过程,Kitchen对象代表 fork 进程和Chefs往往是管理单个、分离的对象 std::thread其中抽象类 Pizza会煮熟,一定时间。

我真的不想用软件设计来打扰你,很明显有一些奇怪或糟糕的选择。给你一个偷看,每个FIFO对象处理系统 FIFO 管道,并且它们全局管理 Reception 之间的 IPC和倍数 Kitchens因为这里的内存不是共享的。

施工后,Reception有自己的管道(旨在接收来自任何 Kitchens 的确认)。这个目前还没有实现,目前肯定不是很有用

Reception必须将订单发送至Kitchens ,这就是为什么 Reception创建一个管道并将其传递给新的 Kitchens FIFO 和厨房都由 unsigned int _internalCount 标识, 每增加一个新的厨房。
std::map<unsigned int, Kitchen> _kitchens将增长以存储任何新创建的厨房和std::map<unsigned int, FIFO> _fifo存储接收的每个 channel ,并使用它们发送数据。
请注意 _internalCount附加到管道名称,这是 './pipes/kitchen_1', './pipes/kitchen_2', etc... 之类的结果
我不知道 StackOverflow 中所有用于简化和明确说明的最佳实践,但下面是 Reception、Kitchen 和 FIFO 类。

#include <fstream>
#include <map>
#include <queue>
#include <string>
#include <vector>

class FIFO {
public:
FIFO() = delete;
FIFO(int channel);
FIFO(const FIFO& copy);
~FIFO();

public:
void operator>>(std::string& container);
void operator<<(const std::string& data);
void operator<<(const char* data);
FIFO& operator=(const FIFO& copy);
void operator()();

public:
std::string readFromChannel();
void sendToChannel(const std::string& data);
void flushChannel();

public:
std::string getFIFOname() const;

private:
void createDataChannel(int type);

private:
std::string _fifoPath;
std::fstream _fifo;
};

class Kitchen {
public:
Kitchen() = delete;
Kitchen(Args args, FIFO newFifo);
~Kitchen();

public:
FIFO getFifo() const;
bool getStatus() const;

private:
void goCooking();
void dispatchOrders();
bool isFree();

private:
Stock _stock;
Time _time;

int _maxChefs;
std::vector<Chef> _chefs;

std::queue<std::string> _orders;
int _currentAssign;

bool _status;
bool _isOpen;

FIFO _channel;
};

class Reception {
public:
Reception() = delete;
Reception(int argc, char** argv);
~Reception();

public:
void openPlazza();
void closePlazza();

public:
void createKitchen(std::string order);
void sendRequest(std::string order, int kitchenNb);
void receiveConfirmation();
unsigned int findKitchenAvailable();

private:
bool _open; /*
OrderManager _checker; ** Encapsulate other
Args _args; ** aspects of the project
Shell _shell; **
Process _process; */

FIFO _mainPipe; // Not very useful here
std::map<unsigned int, FIFO> _fifos;
std::map<unsigned int, Kitchen> _kitchens;
unsigned int _internalCount;
};

int main() {}

如果您需要什么或需要更多详细信息,请告诉我:)


编辑 2:
我在第一行之后添加了一些来自 g++ 的附加错误/注释(显示在初始问题上方)

/usr/include/c++/9/bits/stl_pair.h:436:9: note: candidate: ‘template<class ... _Args1, long unsigned int ..._Indexes1, class ... _Args2, long unsigned int ..._Indexes2> std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>)’
436 | pair(tuple<_Args1...>&, tuple<_Args2...>&,
/usr/include/c++/9/bits/stl_pair.h:436:9: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/stl_pair.h:529:14: note: mismatched types ‘std::tuple<_Tps ...>’ and ‘unsigned int’
529 | return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));

一如既往,提前感谢您的宝贵时间和建议!


编辑 3:
我现在开始把我脑子里的一切联系起来,谢谢大家。那里的事情变得棘手,但我肯定会从你的所有建议中学习。我会再次仔细阅读@reinstate-monica 的答案并在我身边做进一步的研究,我有解决这个问题并让它变得更好的关键。
抱歉不够清晰,我没想到我的第一个问题会如此棘手,我希望它更清楚。
我的下一个问题会更清楚,我会尝试从一开始就制作最少的可重现代码,我会仔细研究 StackOverflow @ted-lyngmo 的良好实践!
感谢大家的支持和时间,照顾好自己!

最佳答案

首先,你应该放置,而不是插入——这样可以避免复制:

_kitchens.emplace(_internalCount, std::move(newKitchen));
_fifos.emplace(_internalCount, std::move(newFIFO));

其次,Kitchen 可能不可复制且不可移动,这就是您的麻烦所在。确保它可以被复制或至少被移动。如果是,你必须展示一个最小的例子。我可以写一个,但它会工作,一个不工作的变体将是微不足道的,没有帮助。因此,请先向我们展示您的作品:)

第三:我不知道您的 FIFO 对象的设计,但是在构造函数中将其临时实例传递给 Kitchen 可能只不过是一个错误。当您执行所有这些工作的函数退出时,Kitchen 将有一个悬空引用。因此,您真正想要的是:

auto fifo_it = _fifos.emplace(std::piecewise_construct, {_internalCount}, {_internalCount}).first;
if (fifo_it.second)
// if the new fifo was actually inserted
_kitchens.emplace(std::piecewise_construct, {_internalCount}, {_args, fifo_it.first->second});

这样一来,厨房就会引用 fifo,它至少有可能活得足够长以发挥作用。

我非常怀疑将 _internalCount 传递给 map 和 map 中包含的对象。这种信息重复通常是一种糟糕的设计味道。

您应该告诉我们您想要实现的目标,并且可能会出现更好的设计。

关于c++ - 在映射中插入一对时,C++ 是否需要额外的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62310584/

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