0
我已經寫了下面的一段代碼:遞歸矢量模板
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "{";
for (auto i=v.begin(); i!=v.end(); ++i) {
os << *i << " ";
}
os << "}";
return os;
}
這經常vector<int>
情況下能正常工作,但我想要做的是這樣的:
vector<vector<int> > v={{1,2},{3,4}}
cout << v; // Should print {{1 2 } {3 4 } }
相反,我得到編譯錯誤(下面的文本,與長長的候選人名單):test.cpp|212|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::vector<std::vector<int> >')|
我以爲模板函數可以使用兩次,遞歸。我錯了嗎?如果不是,是什麼給了?如果是這樣,有沒有辦法使這個通用的沒有重複的代碼?
如果您不明白錯誤消息,則不建議對其進行彙總。 – Yakk
提供[MCVE]。 – Yakk