在我的矩阵实现中,我将vector用作私有成员。我有重载的ostream
运算符,所以我可以从矩阵中打印所有值。在下面的代码中,我首先使用它for_each
来遍历矩阵的“行”。在lambda中,我在此行的末尾指定了一个参数(矩阵“ column”)作为对vector的引用:
std::for_each(p_matrix.m_vector_of_vectors.begin(), p_matrix.m_vector_of_vectors.end(), [& p_out] (std::vector<int> & el)
这会导致错误。这是完整的代码:
#include <iostream>
#include <vector>
#include <algorithm>
class Matrix{
public:
Matrix()
{
m_vector_of_vectors = {{1,2,3},{4,5,6},{7,8,9}};
}
friend std::ostream & operator << (std::ostream & p_out, const Matrix & p_matrix)
{
std::for_each(p_matrix.m_vector_of_vectors.begin(), p_matrix.m_vector_of_vectors.end(), [& p_out] (std::vector<int> & el)
{
std::for_each(el.begin(), el.end(), [& p_out] (int & el2)
{
p_out << el2 << " ";
});
p_out << std::endl;
});
return p_out;
}
private:
std::vector<std::vector<int>> m_vector_of_vectors;
};
int main()
{
Matrix l_matrix;
std::cout << l_matrix;
return 0;
}
这是完整的错误打印:
In file included from /usr/include/c++/5/algorithm:62:0,
from main.cpp:11:
/usr/include/c++/5/bits/stl_algo.h:
In instantiation of ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _Funct = operator<<(std::ostream&, Matrix)::<lambda(std::vector<int>&)>]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',29)">main.cpp:29:10</span>: required from here
/usr/include/c++/5/bits/stl_algo.h:3767:5: error: no match for call to ‘(operator<<(std::ostream&, Matrix)::&)>) (const std::vector&)’
__f(*__first);
main.cpp:22:129: note: candidate: operator<<(std::ostream&, Matrix)::&)>
std::for_each(p_matrix.m_vector_of_vectors.begin(), p_matrix.m_vector_of_vectors.end(), [& p_out] (std::vector<int> & el)
没有参考就可以正常工作:
std::for_each(p_matrix.m_vector_of_vectors.begin(), p_matrix.m_vector_of_vectors.end(), [& p_out] (std::vector<int> el)
有人可以解释为什么这个参考很重要吗?在第二(内部)lambda代码中,无论有没有参考,它都能工作。重载运算符中的p_matrix参数也是如此。我试过循环向量为正态变量的向量:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::vector<int>> a = {{1,2,3},{4,5,6},{7,8,9}};
std::for_each(a.begin(), a.end(), [] (std::vector<int> & el)
{
std::for_each(el.begin(), el.end(), [] (int & el2)
{
std::cout << el2;
});
});
return 0;
}
并且工作正常,因此我假设此错误与重载运算符有关。感谢帮助。
在
friend std::ostream & operator << (std::ostream & p_out, const Matrix & p_matrix)
p_matrix
是const
。也就是说p_matrix.m_vector_of_vectors
,const
这意味着p_matrix.m_vector_of_vectors.begin()
返回a const_iterator
,这意味着传递给lambda的元素是const
。由于您的lambda采用非const引用,因此不兼容。它将剥离const
元素的,这是不允许的。更改lambda以采用const引用,例如
friend std::ostream & operator << (std::ostream & p_out, const Matrix & p_matrix)
{
std::for_each(p_matrix.m_vector_of_vectors.begin(), p_matrix.m_vector_of_vectors.end(), [& p_out] (std::vector<int> const & el) // <- const here
{
std::for_each(el.begin(), el.end(), [& p_out] (int const & el2) //<- const here
{
p_out << el2 << " ";
});
p_out << std::endl;
});
return p_out;
}
然后代码将编译。在第二个示例中,您不需要它,因为a
不是,const
因此它的迭代器并不const
意味着它传递给lambda的元素不是const
。
我还想指出,基于范围的for循环会使代码更加紧凑。您可以更改operator <<
为
friend std::ostream & operator << (std::ostream & p_out, const Matrix & p_matrix)
{
for (auto const& row : p_matrix.m_vector_of_vectors)
{
for (auto const& e : row)
p_out << e << " ";
p_out << "\n";
}
return p_out;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句