问题描述
在linked_list.h 中声明的基类
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
template<class T>
class linked_list
{
private:
linked_list * next_item{nullptr};
linked_list * prevIoUs_item{nullptr};
static int number_of_item;
static linked_list * head;
static linked_list * tail;
protected:
public:
linked_list(/* args */)
{
if (nullptr == head)
{
head = this;
tail = this;
}
else
{
tail->next_item = this;
prevIoUs_item = tail;
tail = this;
}
number_of_item++;
}
~linked_list()
{
number_of_item--;
//If all item is removed :
if (0 == number_of_item)
{
head = nullptr;
tail = nullptr;
}
//If first item in list will be removed :
else if (head == this)
{
head = next_item;
next_item->prevIoUs_item = nullptr;
}
//If last item of list will be removed :
else if (tail == this)
{
tail = prevIoUs_item;
prevIoUs_item->next_item = nullptr;
}
else
{
prevIoUs_item->next_item = next_item;
next_item->prevIoUs_item = prevIoUs_item;
}
}
int get_number_of_item()
{
return (number_of_item);
}
linked_list * get_next_item(void)
{
return (next_item);
}
linked_list * get_prevIoUs_item(void)
{
return (prevIoUs_item);
}
static linked_list * get_first_item(void)
{
return (head);
}
static linked_list * get_last_item(void)
{
return (tail);
}
//return true if desirable item exist in list :
virtual bool is_exist (const T * item) = 0;
};
#endif
并在linked_list.cpp中定义:
#include <linked_list.h>
template <class T>
int linked_list<T>::number_of_item{0};
template <class T>
linked_list<T> * linked_list<T>::head{nullptr};
template <class T>
linked_list<T> * linked_list<T>::tail{nullptr};
在 Command.h 中声明的派生类:
#ifndef COMMAND_H
#define COMMAND_H
#include <Arduino.h>
#include <net_packet.h>
#include <linked_list.h>
#define COMMAND_LENGTH 15
typedef void (*callback_t) (Command_type,String);
class Command : public std::array<char,COMMAND_LENGTH + 1>,protected linked_list<Command>
{
private:
std::array<char,COMMAND_LENGTH + 1> name;
callback_t callback;
public :
Command (const char name[COMMAND_LENGTH + 1],callback_t callback);
~Command();
const std::array<char,COMMAND_LENGTH + 1>& get_name (void);
callback_t get_callback(void);
virtual bool is_exist(const Command *item);
boolean operator==(Command& command);
};
#endif
并在Command.cpp中定义
#include <Arduino.h>
#include <Command.h>
using namespace std;
Command::Command (const char name[COMMAND_LENGTH + 1],callback_t callback)
{
strcpy(this->name.data(),name);
this->callback = callback;
}
Command::~Command()
{
callback = nullptr;
}
const std::array<char,COMMAND_LENGTH + 1>& Command::get_name (void)
{
return (name);
};
callback_t Command::get_callback(void)
{
return (callback);
}
bool Command::is_exist(const Command * item)
{
Command * p;
for (p = static_cast<Command *>(Command::get_first_item()); p != get_last_item(); p = static_cast<Command *>(p->get_next_item()))
{
if (p == item)
{
item = static_cast<const Command *>(p);
return (true);
}
}
return (false);
}
boolean Command::operator==(Command& command)
{
return (0 == strcmp(get_name().data(),command.get_name().data()));
};
main.cpp :
void callback_config_ip(Command_type type,String param);
Command config_ip("ConfigIP",callback_config_ip);
void setup()
{
//Do something ....
}
void loop()
{
//Do something ....
}
void callback_config_ip(Command_type type,String param)
{
// ...
}
当我在 main.cpp 文件中定义“config_ip”对象时发生以下错误:
> Executing task: platformio run <
Processing esp07 (platform: espressif8266; board: esp07; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v,--verbose` option
CONfigURATION: https://docs.platformio.org/page/boards/espressif8266/esp07.html
PLATFORM: Espressif 8266 (2.6.3) > Espressif Generic ESP8266 ESP-07 1MB
HARDWARE: ESP8266 80MHz,80KB RAM,1MB Flash
PACKAGES:
- framework-arduinoespressif8266 3.20704.0 (2.7.4)
- tool-esptool 1.413.0 (4.13)
- tool-esptoolpy 1.30000.201119 (3.0.0)
- toolchain-xtensa 2.40802.200502 (4.8.2)
LDF: Library Dependency Finder -> bit-ly/configure-pio-ldf
LDF Modes: Finder ~ chain,Compatibility ~ soft
Found 30 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <ESP8266Ping> 1.0.0
| |-- <ESP8266WiFi> 1.0
|-- <ESP8266WiFi> 1.0
|-- <ESP8266httpUpdate> 1.3
| |-- <ESP8266HTTPClient> 1.2
| | |-- <ESP8266WiFi> 1.0
| |-- <ESP8266WiFi> 1.0
|-- <EEPROM> 1.0
|-- <LittleFS(esp8266)> 0.1.0
Building in release mode
Linking .pio/build/esp07/firmware.elf
/home/ali/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: .pio/build/esp07/src/Command.cpp.o:(.text._ZN7Command8is_existEPKS_+0x0): undefined reference to `linked_list<Command>::head'
/home/ali/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: .pio/build/esp07/src/Command.cpp.o:(.text._ZN7Command8is_existEPKS_+0x4): undefined reference to `linked_list<Command>::tail'
/home/ali/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: .pio/build/esp07/src/Command.cpp.o:(.text._ZN7CommandC2EPKcPFv12Command_type6StringE+0x4): undefined reference to `linked_list<Command>::number_of_item'
collect2: error: ld returned 1 exit status
*** [.pio/build/esp07/firmware.elf] Error 1
========================================================== [Failed] Took 6.39 seconds ==========================================================
The terminal process "platformio 'run'" terminated with exit code: 1.
Terminal will be reused by tasks,press any key to close it.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)