程序中"..."不用管它
#include ...
class Task {
public:
Task(int id) : id_(id) {}
int getID() { return id_; }
std::string getName() { return "Task"; }
virtual bool checkTimeout() { return false; }
private:
int id_;
};
class UDPTask : public Task {
public:
UDPTask(int id) : Task(id) {}
std::string getName() { return "UDPTask"; }
bool checkTimeout() { ... }
...
};
class TCPTask : public Task {
public:
TCPTask(int id) : Task(id) {}
std::string getName() { return "TCPTask"; }
bool checkTimeout() { ... }
...
};
class TaskManager {
public:
void addTask(Task t) {
task_map_[t.getID()] = t;
}
void removeTask(Task t) {
task_map_.erase(t.getID());
}
void checkTimeout() {
std::map<int, Task>::iterator it = task_map_.begin();
for (; it != task_map_.end(); ++it) {
if (it->second.checkTimeout()) {
printf("%s timeout->remove it", it->second.getName());
task_map_.erase(it);
}
}
}
private:
std::map<int, Task> task_map_;
};
其實報錯的本質原因是,你的成員函數裡面有Task t這樣的形參,總之是這樣一個成員函數出錯了,
void addTask(Task t) {
task_map_[t.getID()] = t;
}
task_map_[t.getID()]是要調用默認構造的,但你卻沒有默認構造...