2016-12-02 1 views
0

は、私はちょうど>ヘッダ・ファイルにそれを宣言しませんでした:(エラー:あなたは私のパブリッククラス変数はスコープで宣言されていなかった意味は何current_millis 'はこのスコープで宣言されていない

IDEのArduinoの1.6.12 のArduinoをタイプ宇野は、これは問題でちょうどそれがそこ

timelyCall.cpp

#include "Arduino.h" 
#include "timelyCall.h" 
timelyCall::timelyCall() { 
    current_millis = millis(); 
} 

timelyCall::~timelyCall() { 

ことを確認する必要はありませんここで/

delete current_millis; 
} 

void timelyCall::callEvery(void (&f)(), int ms) { 
    if (millis() - current_millis > ms) { 
    f(); 
    current_millis = millis(); 
    } 
} 

void setCurrentMillis() { 
    current_millis = millis(); 
} 

エラーtimelyCall.h

class timelyCall { 
    public: 
    timelyCall(); 
    ~timelyCall(); 
    unsigned long current_millis; 
    void callEvery(void (&f)(), int ms); 
    }; 

FullError

Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno" 

sketch\timelyCall.cpp: In destructor 'timelyCall::~timelyCall()': 

timelyCall.cpp:8: error: type 'long unsigned int' argument given to 'delete', expected pointer 

    delete current_millis; 

     ^

sketch\timelyCall.cpp: In function 'void setCurrentMillis()': 

timelyCall.cpp:19: error: 'current_millis' was not declared in this scope 

    current_millis = millis(); 

^

exit status 1 
type 'long unsigned int' argument given to 'delete', expected pointer 

This report would have more information with 
"Show verbose output during compilation" 
option enabled in File -> Preferences. 
+0

あなたは 'new'を'削除 'してください(そして 'delete []'は 'new []'とします)。 'new'を使って何かを動的に割り当てていない場合、それを' delete'してはいけません。 –

答えて

1

current_millsを削除しないでくださいデストラクタで。動的にasigned変数を削除する必要があります。言い換えれば、新しいオブジェクトを作成した場合、1:1の比率で削除して削除します。

timelyCallクラス内に定義されたsetCurrentMills()ファンクションは2番目のエラーはありません。

class timelyCall { 
    public: 
    timelyCall(); 
    ~timelyCall(); 
    unsigned long current_millis; 
    void setCurrentMills(); 
    void callEvery(void (&f)(), int ms); 
    }; 

、その後void timelyCall::setCurrentMills()する関数定義を変更します。

あなたはクラス定義を変更する必要があります。

また、割り当てたい値を関数setに渡すための標準です。だから、void setCurrentMills(const unsigned long& mills);にこの署名を変更し、代わりにcurrent_millsにfuncion mills()の戻り値を割り当てるので、デストラクタでcurrent_millisを削除しないでください。このobject.setCurrentMills(mills());

1

のように、この関数の戻り値でセッターを呼び出すことができ、将来の変更のために本当に有益であろうnewを使用して作成したものだけを削除します。

関連する問題