我有下面的代碼漂亮地打印通用矢量 - :漂亮的印刷嵌套向量
// print a vector
template<typename T1>
std::ostream& operator <<(std::ostream& out, const std::vector<T1>& object)
{
out << "[";
if (!object.empty())
{
std::copy(object.begin(), --object.end(), std::ostream_iterator<T1>(out, ", "));
out << *--object.end(); // print the last element separately to avoid the extra characters following it.
}
out << "]";
return out;
}
我得到一個編譯錯誤,如果我試圖從它打印嵌套向量。
int main()
{
vector<vector<int> > a;
vector<int> b;
// cout << b ; // Works fine for this
cout << a; // Compiler error
}
我正在使用GCC 4.9.2和-std=c++14
標誌。
由編譯器給定的錯誤信息是: -
no match for 'operator<<' (operand types are 'std::ostream_iterator<std::vector<int>, char, std::char_traits<char>::ostream_type {aka std::basic_ostream<char>}' and 'const std::vector<int>')