2012-04-28 13 views
1

私はその原因を知っていますが、解決策はわかりません。`Scheduler :: _ singleton 'の多重定義で、ヘッダを2回インポートしています

私は、問題はそのother.cppにあるヘッダファイル

#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

小さなシングルトンクラスを持っていますどのように私はその周りに行く - 私は二度同じヘッダをインポートするので、私はそのを知っ

../otherfile.cpp:47: multiple definition of `Scheduler::_singleton' 

を取得しておきますか? _singletoneは静的であり、静的なままでなければなりません。 そして、インクルードガードがどうして助けにならないのですか?

+0

[この質問への回答](http://stackoverflow.com/questions/10340515/wunused-variable-warning)私は 'Scheduler :: _ singleton'の定義を実装ファイルに入れることを提案しました。私はそれが問題を解決すると思う。 – juanchopanza

答えて

0

移動し、このラインにあなたのCPPファイルの正確1:

Scheduler * Scheduler::_singleton = 0; 
+0

実際これはクラス 'Scheduler.cpp'に対応する実装ファイルの方が良いでしょう。 – dirkgently

+0

@dirkgentlyちょっとのようなものが最近提案されました[ここ](http://stackoverflow.com/questions/10340515/wunused-variable-warning):-) – juanchopanza

+0

ああ!そこから私のコメントを投稿することを提案していますか?私はそれを見なかった。 – dirkgently

2

_singletonがあなたのクラスのstaticメンバーであり、あなたはクラス宣言の外で明示的に定義する必要があります。ヘッダーで(そうしたように)ヘッダーを複数のソースファイルに含めると、リンカーは同じシンボルの複数の定義を見つけます。したがって、それは不平です。だから解決策は、この静的メンバー定義を対応するソースファイルに移動することです。

関連する問題