c++ - 将初始化向量作为参考传递时出错
问题描述
我有一个功能:
std::string GraphList::_dfs(int src, std::vector<int>& visited)
我这样称呼它:
std::vector<bool> visited(NUM_V, false);
return _dfs(src, visited);
我收到以下错误:
a reference of type "std::vector<int, std::allocator<int>> &" (not const-qualified) cannot be initialized with a value of type "std::vector<bool, std::allocator<bool>>"
如果我传递一个未初始化的向量,它可以正常工作,但是为什么当我传递一个初始化的向量时编译器会抱怨?
解决方案
您将 avector<bool>
作为参数传递给一个函数,该函数需要 avector<int>
对应的参数,该参数是不同的类型,两种类型之间不可能进行隐式转换,因此会出现错误。
推荐阅读
- pandas - pandas to_datetime 不接受“24”作为时间
- nginx - Nginx RTMP 模块输出 HLS 未找到
- typescript - 类型注释和区分字符串文字的联合
- php - 使用 PHP 从 MySQL 下载定制的 excel
- forms - 当前不使用验证码的在线表单最好的反垃圾邮件解决方案是什么?
- javascript - 如何使用 javascript 推送评论?
- r - 选择另一个标记后,如何清除已添加到传单地图的标记?
- iis - 诊断在 IIS 上运行的网站中的零星锁定
- java - 当我尝试在 JPQL 中使用 Case 语句时出现问题
- python - 比较不同 pandas 数据集中的 2 列,如果值存在于第二个数据集中,则替换值