class foo {
public:
friend ostream& operator << (ostream &os, const foo &f);
foo(int n) : a(n) {}
private:
vector <int> a;
};
ostream& operator << (ostream &os, const foo &f) {
for (int i = 0; i < f.a.size(); ++i)
os << f.a[i] << " ";
os << endl; // why is this line a must?
}
int main(void) {
foo f(2);
cout << f << endl;
return 0;
}
In dem obigen Code, wenn die markierte Zeile entfernt wird, wird es einen Segment Fehler Fehler, kann jemand erklären, warum?Überlasteter ostream operator segmentation fault wenn kein endl
Warum niemand den Code in Frage zu überprüfen cares? Der Compiler sollte Sie vor einem solchen Fehler gewarnt haben - "... Warnung: Keine Rückgabeanweisung in der Funktion, die nicht void [-Wreturn-type] zurückgibt" Siehe http://liveworkspace.org/code/2ygK20$1 } ^ – SChepurin