【发布时间】:2020-09-30 06:46:26
【问题描述】:
你好伟大的 StackOverflow 社区!
我正在制作一些 C++,但在 std::map 中插入元素时遇到了问题。
这里有 2 个映射,存储一个类似 ID 的 unsigned int 作为键,另一个对象作为值:
std::map<unsigned int, FIFO> _fifos;
std::map<unsigned int, Kitchen> _kitchens;
两个映射都是 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 对象代表分叉进程,Chefs 往往是管理单个分离std::thread 的对象,其中抽象类Pizza 将煮熟,一段时间。
我真的不想用软件设计来打扰你,很明显有一些奇怪或糟糕的选择。给你看一下,每个FIFO 对象都处理一个系统FIFO 管道,它们全局管理Reception 和多个Kitchens 之间的IPC,因为这里的内存不共享。
在构建时,Reception 拥有自己的管道(旨在接收来自任何Kitchens 的确认)。这个目前还没有实现,目前肯定不是很有用
Reception 必须向Kitchens 发送订单,这就是为什么Reception 创建一个管道并将其传递给新的Kitchens,FIFO 和厨房都由unsigned int _internalCount 标识,每个新厨房都会增加。 std::map<unsigned int, Kitchen> _kitchens 将增长到存储创建的任何新厨房,std::map<unsigned int, FIFO> _fifo 存储每个接收通道,并使用它们发送数据。
请注意,_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 的良好实践!
感谢大家的支持和时间,照顾好自己!
【问题讨论】:
-
我们可能需要了解更多关于
Kitchen的信息,看看它是否对复制此类有限制... -
其余的错误信息在哪里?您已经删除了告诉我们问题所在的错误部分:(
-
你好@MooingDuck,有很多错误来自C++的地下。在g++的消息之后(在最初的帖子中),有两个有趣但我不太清楚的错误:
c++ -
@Charlie 如果您创建minimal reproducible example,我们会更容易提供帮助。删除重现问题所必需的所有内容,并包括重现问题所需的所有内容。
Args类型似乎很重要——但您没有包含它的定义。你也错过了很多标题。我现在使它更接近mcve。请填空。