微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

“未定义引用”已定义的静态成员变量和“collect2:错误:ld 返回 1 个退出状态”

如何解决“未定义引用”已定义的静态成员变量和“collect2:错误:ld 返回 1 个退出状态”

我有以下文件和类:

在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 举报,一经查实,本站将立刻删除。