我有以下代码来覆盖<<运营商
到目前为止,我已将错误定位为++它。每当涉及到这段代码时,我的程序都会崩溃(SIGTERM)。
ostream& operator<<(ostream& os, const Kooi& kooi) {
map<int, Dier*>::const_iterator it;
for (it = kooi.de_dieren->begin(); it != kooi.de_dieren->end(); ++it) {
os << it->second << endl;
}
}
return os;
编辑:Kooi类的完整头文件:
#ifndef kooi_H
#define kooi_H 1.3
#include <string> // voor: std::string
#include <iostream> // voor: std::ostream
#include <map> // voor: std::map<K,V>
#include <iterator> // voor: next
#include "Dier.h" // voor: class Dier
/** @class Kooi Kooi.h
* A cage class to hold animals of the same type.
*/
class Kooi
{
// The Kooi output operator to print the cage.
// It also prints any animals within.
friend std::ostream& operator<<(std::ostream&, const Kooi&);
private:
std::string de_diersoort; // the animal category
std::map<int, Dier*> *de_dieren; // the animal container
public:
/// Construct a cage for the given type of animals
/// @param diersoort The animal category
/// @pre diersoort must be a non-empty string
Kooi(const std::string& diersoort);
/// Destroy a cage, any animals within should also be destroyed
~Kooi();
/// Return the animal category of this cage
/// @return The animal category
inline const std::string& getSoort() const {
return de_diersoort;
}
/// Add an animal to this cage
/// @param nummer The unique number of the animal
/// @pre That animal number may not already exist in this cage
/// @param dier The animal to be added
/// @pre The type of that animal must match with the type of this cage
void addDier(int nummer, Dier *dier);
/// Lookup an animal in this cage
/// @param nummer The number of the animal sought
/// @pre That animal number must be >= 0
/// @returns 0 when the animal is not in this cage
Dier *getDier(int nummer) const;
/// Remove an animal from this cage
/// @param nummer The number of the animal to be removed
/// @pre That animal number must exist in this cage
void erase(int nummer);
/// Tell the number of animals in this cage
/// (used e.g.to calculate the salary of a animalkeeper)
inline int size() const {
return de_dieren->size();
}
// =====================================
/// Save this cage (and the animals) in a file
void saveFile(std::ostream& out) const;
};
/// Kooi output operator (prints both the cage and all animals within)
std::ostream& operator<<(std::ostream& os, const Kooi& kooi);
#endif /*kooi*/
// vim:sw=4:ai:aw:ts=4:
此外,由于某种原因,我100%肯定该程序在++it
崩溃。
请注意,此代码是学校作业的一部分,因此我们不应该过多地使用代码。此作业的范围仅为kooi.cc文件