- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
伟大的 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/
我正在尝试用 C 语言编写一个使用 gstreamer 的 GTK+ 应用程序。 GTK+ 需要 gtk_main() 来执行。 gstreamer 需要 g_main_loop_run() 来执行。
我已经使用 apt-get 安装了 opencv。我得到了以下版本的opencv2,它工作正常: rover@rover_pi:/usr/lib/arm-linux-gnueabihf $ pytho
我有一个看起来像这样的 View 层次结构(基于其他答案和 Apple 的使用 UIScrollView 的高级 AutoLayout 指南): ScrollView 所需的2 个步骤是: 为 Scr
我尝试安装 udev。 udev 在 ./configure 期间给我一个错误 --exists: command not found configure: error: pkg-config and
我正在使用 SQLite 3。我有一个表,forums,有 150 行,还有一个表,posts,有大约 440 万行。每个帖子都属于一个论坛。 我想从每个论坛中选择最新帖子的时间戳。如果我使用 SEL
使用 go 和以下包: github.com/julienschmidt/httprouter github.com/shwoodard/jsonapi gopkg.in/mgo.v2/bson
The database仅包含 2 个表: 钱包(100 万行) 事务(1500 万行) CockroachDB 19.2.6 在 3 台 Ubuntu 机器上运行 每个 2vCPU 每个 8GB R
我很难理解为什么在下面的代码中直接调用 std::swap() 会导致编译错误,而使用 std::iter_swap 编译却没有任何错误. 来自 iter_swap() versus swap() -
我有一个非常简单的 SELECT *用 WHERE NOT EXISTS 查询条款。 SELECT * FROM "BMAN_TP3"."TT_SPLDR_55E63A28_59358" SELECT
我试图按部分组织我的 .css 文件,我需要从任何文件访问文件组中的任何类。在 Less 中,我可以毫无问题地创建一个包含所有文件导入的主文件,并且每个文件都导入主文件,但在 Sass 中,我收到一个
Microsoft.AspNet.SignalR.Redis 和 StackExchange.Redis.Extensions.Core 在同一个项目中使用。前者需要StackExchange.Red
这个问题在这里已经有了答案: Updating from Rails 4.0 to 4.1 gives sass-rails railties version conflicts (4 个答案) 关
我们有一些使用 Azure DevOps 发布管道部署到的现场服务器。我们已经使用这些发布管道几个月了,没有出现任何问题。今天,我们在下载该项目的工件时开始出现身份验证错误。 部署组中的节点显示在线,
Tip: instead of creating indexes here, run queries in your code – if you're missing any indexes, you
你能解释一下 Elm 下一个声明中的意思吗? (=>) = (,) 我在 Elm architecture tutorial 的例子中找到了它 最佳答案 这是中缀符号。实际上,这定义了一个函数 (=>
我需要一个 .NET 程序集查看器,它可以显示低级详细信息,例如元数据表内容等。 最佳答案 ildasm 是 IL 反汇编程序,具有低级托管元数据 token 信息。安装 Visual Studio
我有两个列表要在 Excel 中进行比较。这是一个很长的列表,我需要一个 excel 函数或 vba 代码来执行此操作。我已经没有想法了,因此转向你: **Old List** A
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
我正在学习 xml 和 xml 处理。我无法很好地理解命名空间的存在。 我了解到命名空间帮助我们在 xml 中分离相同命名的元素。我们不能通过具有相同名称的属性来区分元素吗?为什么命名空间很重要或需要
我搜索了 Azure 文档、各种社区论坛和 google,但没有找到关于需要在公司防火墙上打开哪些端口以允许 Azure 所有组件(blob、sql、compute、bus、publish)的简洁声明
我是一名优秀的程序员,十分优秀!