首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么编译错误“未定义的引用”

为什么编译错误“未定义的引用”
EN

Stack Overflow用户
提问于 2013-10-23 14:16:03
回答 1查看 998关注 0票数 1

我有一个类并按下面的方式使用它,但是当我编译它时,我得到了错误“对'RemoteControl::key‘的未定义的引用,我没有起诉为什么,有人帮助我吗?我认为这个类非常简单,但是我不知道为什么编译是错误。

代码语言:javascript
复制
#include <lnp/lnp.h>
#include <conio.h>  // for the delay() function
#include <remote.h>
#include <sys/program.h>
#include <c++/Motor.H>
#include <c++/Sound.H>

extern int remote_control_handler(unsigned int etype, unsigned int key);
extern int checkMessage(int argc, char **argv);

class RemoteControl {
public:
    static unsigned int key;

    static const void init() {
        lr_set_handler(remote_control_handler);
        execi(&checkMessage, 0, 0, PRIO_NORMAL, DEFAULT_STACK_SIZE);
    }
};

wakeup_t remote_control_pressed_message(wakeup_t data) {
    return lnp_rcx_message >= 1 && lnp_rcx_message <= 3;
}

int checkMessage(int argc, char **argv) {
    while (!shutdown_requested()) {
        clear_msg();
        wait_event(&remote_control_pressed_message, 0);

        if (lnp_rcx_message == 1) {
            RemoteControl::key = LRKEY_M1;
        }
        else if (lnp_rcx_message == 2) {
            RemoteControl::key = LRKEY_M2;
        }
        else if (lnp_rcx_message == 3) {
            RemoteControl::key = LRKEY_M3;
        }
    }
    return 0;
}

 int remote_control_handler(unsigned int etype, unsigned int key) {
     RemoteControl::key = 0;

     if (etype == LREVT_KEYON) {
         switch(key) {
             case LRKEY_STOP:
             case LRKEY_BEEP:
             case LRKEY_A1:
             case LRKEY_A2:
             case LRKEY_B1:
             case LRKEY_B2:
             case LRKEY_C1:
             case LRKEY_C2:
                 RemoteControl::key = key;
                 break;

             default:
                 RemoteControl::key = 0;
                 break;
        }
    }
    return 1;
}

wakeup_t remote_control_pressed_key(wakeup_t data) {
    return RemoteControl::key == data;
}

wakeup_t remote_control_pressed(wakeup_t data) {
    return RemoteControl::key != 0;
}

int main(int argc, char **argv) {
    RemoteControl::init();
    Motor *m = new Motor(Motor::B);

    while (!shutdown_requested()) {
        RemoteControl::key = 0;
        clear_msg();
    wait_event(&remote_control_pressed, 0);

        if (RemoteControl::key == LRKEY_STOP) {
            program_stop(1);
        }
        else if (RemoteControl::key == LRKEY_A1) {
            m->forward();
        }
        else if (RemoteControl::key == LRKEY_M1) {
            Sound::beep();
        }
    }
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2013-10-23 14:19:55

您只声明静态成员变量key,但从不定义它。

它必须在类之外单独定义(以及在源文件中):

代码语言:javascript
复制
unsigned int RemoteControl::key;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19544246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档