我有一个小问题,我知道它的原因,但不知道它的解决方案。
我有一个小单例类,其头文件是
#ifndef SCHEDULER_H_
#define SCHEDULER_H_
#include <setjmp.h>
#include <cstdlib>
#include <map>
#include <sys/time.h>
#include <signal.h>
#include "Thread.h"
class Scheduler {
public:
static Scheduler * instance();
~Scheduler();
int threadSwitcher(int status, bool force);
Thread * findNextThread(bool force);
void runThread(Thread * nextThread, int status);
void setRunThread(Thread * thread);
void setSleepingThread(Thread * thread);
void setTimer(int num_millisecs);
std::map<int, Thread *> * getSuspendedThreads() const;
std::map<int, Thread *> * getReadyThreads() const;
Thread * getSleepingThread() const;
Thread * getRunningThread() const;
Thread * getThreadByID(int tid) const;
const itimerval * getTimer() const;
const sigset_t * getMask() const;
void pushThreadByStatus(Thread * thread);
Thread * extractThreadByID(int tid);
private:
Scheduler();
sigset_t _newMask;
sigjmp_buf _image;
static Scheduler * _singleton;
std::map<int, Thread *> * _readyThreads;
std::map<int, Thread *> * _suspendedThreads;
Thread *_sleepingThread;
Thread * _runThread;
itimerval _tv;
};
Scheduler * Scheduler::_singleton = 0;
#endif /* SCHEDULER_H_ */
现在当然我将这个头文件导入Scheduler.cpp
到另一个文件中other.cpp
问题是在 other.cpp 我不断得到
../otherfile.cpp:47: multiple definition of `Scheduler::_singleton'
我知道它是因为我两次导入相同的标题 - 我该如何解决?_singletone
是静态的,必须保持静态。为什么包括警卫没有帮助?