繁体   English   中英

初始化私有静态变量

[英]Initialize private static variable

我在使用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() {
}

您将必须定义_handlers

TinyWebServer::PathHandler ShutterWebserver::_handlers[]

代替

TinyWebServer::PathHandler _handlers[]

shutterWebserver.cpp

这个定义

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的全局变量

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

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];
//...

否则,不清楚数组是否确实具有两个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM