-1

以下のように wake_event でクラス メソッド ポインタを使用したいのですが、コンパイル エラーが発生します。このクラスのインスタンスが複数ある可能性があるため、静的メンバとメソッドを使用したくありません。可能ですか? ソースは何を変更できますか?

class UncouplerController {
private:
    TouchSensor *touchSensor;
public:
    wakeup_t touched(wakeup_t data) {
        return UncouplerController::touchSensor->pressed();
    }

   const void uncoupling() {
       wait_event(&touched, 0);
   }
}

コンパイルエラーは以下の通り

/usr/local/bin/h8300-hitachi-hms-g++ -DCXX -fno-rtti -fno-exceptions -O2 -fno-builtin -fomit-frame-pointer -Wall -I/brickos/include -I/brickos/include/lnp -I. -I/brickos/boot  -c rcx1.C -o rcx1.o
In file included from rcx1.H:27,
                 from rcx1.C:21:
UncouplerController.H: In function `static wakeup_t UncouplerController::untouched(long unsigned int)':
UncouplerController.H:38: member `UncouplerController::touchSensor' is non-static but referenced as a static member
UncouplerController.H:62: at this point in file
UncouplerController.H:63: warning: control reaches end of non-void function `UncouplerController::untouched(long unsigned int)'
UncouplerController.H: In method `const void UncouplerController::uncoupling()':
UncouplerController.H:74: converting from `wakeup_t (UncouplerController::*)(long unsigned int)' to `wakeup_t (*)(long unsigned int)'
UncouplerController.H: In method `const void UncouplerController::coupling()': 
UncouplerController.H:99: converting from `wakeup_t (UncouplerController::*)(long unsigned int)' to `wakeup_t (*)(long unsigned int)'
UncouplerController.H: At top level:
UncouplerController.H:112: `class TouchSensor * UncouplerController::touchSensor' is not a static member of `class UncouplerController'
4

3 に答える 3

0

ここに私のコードがあります、それは動作します。

class UncouplerController {
private:
    static TouchSensor *touchSensor;

public:
    UncouplerController(Sensor::Port sensorPort) {
        touchSensor = new TouchSensor(sensorPort);
    }
    ~UncouplerController() {
        delete touchSensor;
    }
    static wakeup_t touched(wakeup_t data) {
        return UncouplerController::touchSensor->pressed();
    }
    static wakeup_t untouched(wakeup_t data) {
        return !UncouplerController::touchSensor->pressed();
    }
    const void uncoupling() {
        wait_event(&UncouplerController::touched, 0);
        delay(UNCOUPLING_TIME1);
    }
    const void coupling() {
        wait_event(&UncouplerController::touched, 0);
        wait_event(&UncouplerController::untouched, 0);  
        delay(COUPLING_TIME);
    }
};

TouchSensor *UncouplerController::touchSensor;

静的メンバーまたは関数が使用されていないかどうかを知りたいのですが、それらをすべてクラス外に移動すると、同じ結果が得られます!! 誰かが私にどちらの方法が良いか教えてくれますか? ご協力いただきありがとうございます。

于 2013-10-30T15:48:23.213 に答える
0

私はヨアヒムに同意します。一方で、コードのヒントをいくつか紹介します。うまくいくかどうか教えてください。

行を変更します。

return UncouplerController::touchSensor->pressed();

に:

return this->touchSensor->pressed();

wait_event はスタンドアロン関数を調べています。クラス メンバーである関数を渡しているため、出力エラーに次の行が表示されます。

UncouplerController.H:74: converting from `wakeup_t (UncouplerController::*)(long unsigned int)' to `wakeup_t (*)(long unsigned int)'

そのため、スタンドアロン関数へのポインターを返すために touched を再定義し、params を使用してその動作を変更できます。または、wakeup_t をスタンドアロン関数として宣言し、クラスのフレンドにします。2番目のものはかなり簡単に見えます。

うまくいったかどうか教えてください。さらにコードを投稿すると、さらに役立つ可能性があります。

もう 1 つの方法は、キーワード「reinterpret_cast」を使用することですが、お勧めしません。

次のようなものを試すことができます:

// Define the type expected by  "wait_event"
typedef wakeup_t (*wake_func_ptr) (wakeup_t data);


class UncouplerController {

private:
    TouchSensor *touchSensor;

public:
    // Member for pointing the target functions.
    wake_func_ptr touched;
    wake_func_ptr untouched;

    UncouplerController(Sensor::Port sensorPort)
    {
        // This will rise a lot of warnings.
        // I strongly not recomend to do this.
        // But it seems to be a solution.
        touched = reinterpret_cast<wake_func_ptr>(&UncouplerController::touched_func);
        untouched = reinterpret_cast<wake_func_ptr>(&UncouplerController::untouched_func);

        touchSensor = new TouchSensor(sensorPort);
    }


    ~UncouplerController() {
        delete touchSensor;
    }

    wakeup_t touched_func(wakeup_t data) {
        return touchSensor->pressed();
    }

    wakeup_t untouched_func(wakeup_t data) {
        return touchSensor->pressed();
    }


    const void uncoupling() {
        wait_event(touched, 0);
        delay(UNCOUPLING_TIME1);
    }

    const void coupling() {
        wait_event(touched, 0);
        wait_event(untouched, 0);
        delay(COUPLING_TIME);
    }
};

// The purpose of this line isn't clear for me.
TouchSensor *UncouplerController::touchSensor;
于 2013-10-30T12:43:31.703 に答える