首页 > 解决方案 > 将初始化向量作为参考传递时出错

问题描述

我有一个功能:

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>>"

如果我传递一个未初始化的向量,它可以正常工作,但是为什么当我传递一个初始化的向量时编译器会抱怨?

标签: c++stl

解决方案


您将 avector<bool>作为参数传递给一个函数,该函数需要 avector<int>对应的参数,该参数是不同的类型,两种类型之间不可能进行隐式转换,因此会出现错误。


推荐阅读