2016-06-24 4 views
0
class Stepper 
{ 
    private: 
    int enable; 
    int direction; 
    int speed; 
    static int turretCounter; 
    public: 
    Stepper(int en, int dir) 
    { 
     enable = en; 
     direction = dir; 
     pinMode(enable,OUTPUT); 
     pinMode(direction,OUTPUT); 
     pinMode(4,OUTPUT);  
    } 
    void Stop() 
    { 
     digitalWrite(enable,1); 
     Timer1.detachInterrupt(); 
     Timer1.stop(); 
    } 
    static void IncrementCounter() 
    { 
     turretCounter++; 
    } 
    //your step pin must be 4 due to limitations on static vars and methods 
    static void Step() 
    { 
     digitalWrite(4,0); 
     delayMicroseconds(1); 
     digitalWrite(4,1); 
     IncrementCounter(); 
    } 
    void SetSpeed(int Speed) 
    { 
     speed = Speed; 
    } 
    void Run(int seconds) 
    { 
     digitalWrite(enable,0); 
     Timer1.attachInterrupt(Step); 
     Timer1.initialize(speed); 
    } 
    int GetCounter() 
    { 
     return turretCounter; 
    } 
    void SetDirection(int dir) 
    { 
     digitalWrite(direction,dir); 
    } 
    int GetSpeed() 
    { 
     return speed; 
    } 
}; 

정적 Step() 메서드 및 정적 IncrementCounter() 메서드에주의를 환기하고 싶습니다. Step() 함수 내에서 변수를 증가 시키려고 시도했지만 항상 "Arudino Nano 보드에서 컴파일하는 중 오류가 발생합니다". Step() 메서드 내에서 직접 IncrementCounter()를 호출하려고 시도했는데 "개체가없는 클래스 메서드를 호출 할 수 없습니다"오류가 발생합니다. Step() 메서드에서이 오류가 발생합니다.정적 클래스 함수 내에서 정적 변수를 증가시킬 수 없습니다.

+3

* 개체가없는 클래스 메서드를 호출 할 수 없습니다 * 코드에서 오류가 발생한 위치를 가리켜 야합니다. 그 코드는 무엇입니까? – NathanOliver

+0

그 오류는 Step() 메서드에서 발생합니다. @ NathanOliver –

답변

1

하는 곳 turretCounter을 정의하십시오 (바람직하게는 .cpp/.cxx/.C 파일)이 같은 :

int Stepper::turretCounter = 0; 

이 방법으로 문제가 해결 될 경우, 다음 아마 당신은 더 많은 정보를 오류 메시지가 간과하고 마지막 게시 컴파일러 출력의 행. 미래에는 컴파일 오류에 더 많은주의를 기울여야하며, 출력에 나타나는 순서대로 연구하는 습관을들이십시오 (이후의 오류는 이전 오류의 결과 일 수 있기 때문에).

관련 문제