初始化私有静态变量

时间:2015-12-25 14:32:53

标签: c++ static arduino

我在使用IDE 1.6.7的arduino M0 pro上 我试图初始化变量但是我有一个编译错误:

Arduino : 1.6.7 (Mac OS X), Carte : "Arduino/Genuino Zero (Programming Port)"

Attention: platform.txt du cœur 'Arduino SAMD (32-bits ARM Cortex-M0+) Boards' contiens recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{build.path}/{archive_file}" "{object_file}" dépassé, converti automatiquement en recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}". La mise a niveau de ce cœur est conseillée.
libraries/shutter_webserver/shutterWebserver.cpp.o: In function _GLOBAL__sub_I__ZN16ShutterWebserverC2Ev':
/Users/pyrotecnix/Documents/Arduino/libraries/shutter_webserver/shutterWebserver.cpp:29: undefined reference to `ShutterWebserver::_handlers'
collect2: error: ld returned 1 exit status
exit status 1
Erreur lors de la compilation.

shutterWebserver.h

#ifndef ShutterWebserver_h
#define ShutterWebserver_h

#include <pins_arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Flash.h>
#include <TinyWebServer.h>

class ShutterWebserver {

  public:

    /*Constructeur*/
    ShutterWebserver();

    static void init();
    static void run();
    static boolean index_handler(TinyWebServer& web_server);

  private:
    static TinyWebServer::PathHandler _handlers[2];
    static TinyWebServer _web;


};
#endif

shutterWebserver.cpp

#include "shutterWebserver.h"


ShutterWebserver::ShutterWebserver() {}

TinyWebServer::PathHandler _handlers[] = {
    //Register the index_handler for GET requests on /
    {"/", TinyWebServer::GET, &ShutterWebserver::index_handler },
    {NULL}, // The array has to be NULL terminated this way
};
TinyWebServer ShutterWebserver::_web = TinyWebServer(_handlers, NULL);

boolean ShutterWebserver::index_handler(TinyWebServer& web_server) {
  web_server.send_error_code(200);
  web_server << F("<html><body><h1>Hello World!</h1></body></html>\n");
  return true;
}

void ShutterWebserver::init() {
}

void ShutterWebserver::run() {
}

2 个答案:

答案 0 :(得分:2)

您必须将_handlers定义为

TinyWebServer::PathHandler ShutterWebserver::_handlers[]

而不是

TinyWebServer::PathHandler _handlers[]
shutterWebserver.cpp中的

答案 1 :(得分:1)

这个定义

TinyWebServer::PathHandler _handlers[] = {
    //Register the index_handler for GET requests on /
    {"/", TinyWebServer::GET, &ShutterWebserver::index_handler },
    {NULL}, // The array has to be NULL terminated this way
};

声明一个名为_handlers

的全局变量

如果要定义类数据成员,则必须指定变量的quakified名称。

TinyWebServer::PathHandler ShutterWebserver::_handlers[] = {
    //Register the index_handler for GET requests on /
    {"/", TinyWebServer::GET, &ShutterWebserver::index_handler },
    {NULL}, // The array has to be NULL terminated this way
};

最好明确指定数组的大小

TinyWebServer::PathHandler ShutterWebserver::_handlers[2] = {
    //Register the index_handler for GET requests on /
    {"/", TinyWebServer::GET, &ShutterWebserver::index_handler },
    {NULL}, // The array has to be NULL terminated this way
};

因为在类定义中它已经明确设置

//...
private:
  static TinyWebServer::PathHandler _handlers[2];
//...

否则不清楚阵列是否确实有两个元素。