编译错误:"未定义引用AList <Pwm> :: AList()"Arduino C++

 我只爱庄宝贝 发布于 2023-02-13 15:12

我正在使用AList库为Arduino创建一个双向链表,但在定义它时会遇到编译错误.该库不是由我编写的,其他人已成功使用它,所以我认为这不是问题,但我的代码就是问题.我也在使用Arduino IDE,所以我认为它不是链接器错误.这是我的主文件(main.ino):

 #include "project.h"
 //other stuff

project.h:

#include "Arduino.h"
#include "AList.h"
#include "PWM.h"
//other stuff

pwm.h中:

#ifndef PWM_H
#define PWM_H

class Pwm{
  public:
  static AList pwms;
  static int numPwms;
  //other stuff
};

#endif

PWM.cpp:

#include "project.h"

int Pwm::numPwms = 0;
AList Pwm::pwms;
//other stuff

AList.h:

#ifndef ALIST_H
#define ALIST_H

template 
class AList{

private:
    /** Intended for private representation of a ListItem within the AList class - Internal use only!
    @author Marco Bertschi
    */
    struct PrivateListItem{
        PrivateListItem* prv;
        PrivateListItem* nxt;
        ListItem crnt;
    };

    PrivateListItem* last;      //!< The last item of the list.
    PrivateListItem* first;     //!< The first item of the list.
    int count;                  //!< Zero-based count of items within the list.

public:
    AList();
    ~AList();
    ListItem First();
    ListItem Last();

    int Count();
    void Add(ListItem listItem);
    void RemAt(int index);
    ListItem GetAt(int index);
    void Clr();
};

#endif //ALIST_H

最后,AList.cpp:

#include "AList.h"

//! Instantiates a new instance of an AList.
/*!
\return     AList     A new instance of an AList.
*/
template 
AList::AList(){
    count = -1;
}
//! Destroys the instance of AList.
/*!
The AList::Clr() is called in order to free memory which
was previously occupied by the dynamically allocated list items.
\sa Clr();
*/
template 
AList::~AList(){
    if (count > -1){
        Clr(); //Clear the List in order to free memory
    }
}
//! Adds an Item of the type ListItem to the AList.
/*!
\param      li      [ListItem]      The ListItem which is added to the AList.
\return     void    [void]      
*/
template 
void AList::Add(ListItem li){
    PrivateListItem* pLItem = new PrivateListItem;
    pLItem->crnt = li;

    if (count > -1){
        pLItem->nxt = first;
        pLItem->prv = last;
        last->nxt = pLItem;
        last = pLItem;
        count++;
    }
    else if (count == -1){
        first = pLItem;
        first->nxt = pLItem;
        first->prv = pLItem;
        last = pLItem;
        last->nxt = pLItem;
        last->prv = pLItem;
        count = 0;
    }
}
//! Removes a ListItem from a given index position in the AList.
/*!
In case that there is no ListItem stored at the given index of the List 
no operation will be done and the list remains unchanged.

\param      index   [int]   The Index at which the ListItem gets removed.
\return     void    [void]
*/
template 
void AList::RemAt(int index){
    if (index < count){
        PrivateListItem* pLItem = last;
        for (int i = 0; i <= index; i++){
            pLItem = pLItem->nxt;
        }
        pLItem->prv->nxt = pLItem->nxt;
        pLItem->nxt->prv = pLItem->prv;
        delete pLItem;
        count--;
    }
}
//! Gets a ListItem from a given index position in the AList.
/*!
In case that there is no ListItem stored at the given index of the List
this method will return a random value, or may lead to a Memory read exception.
This also applies if no item at all is stored in the list.

\param      index       [int]       The Index at which the ListItem gets removed.
\return     ListItem    [ListItem]  The ListItem at the position `index` in the list.
\sa Count()
*/
template 
ListItem AList::GetAt(int index){
    PrivateListItem* pLItem = first;
    if (index <= count && index > -1){
        int i = 0;
        while(i < index){
            pLItem = pLItem->nxt;
            i++;
        }
    }

    return pLItem->crnt;
}
//! Gets the first ListItem which is stored in the list.
/*!
A random value will be returned if no items are stored in the list.

\return     ListItem    [ListItem]    The first ListItem in the list.
\sa Last(), Count()
*/
template 
ListItem AList::First(){
    return first->crnt;
}
//! Gets the last ListItem which is stored in the list.
/*!
A random value will be returned if no items are stored in the list.
If there is only one Item stored in the list this method returns the same value as AList::First().

\return     ListItem    [ListItem]  The first ListItem in the list.
\sa First(), Count()
*/
template 
ListItem AList::Last(){
    return last->crnt;
}
//! Gets the number of ListItems in the List.
/*!
The number is zero-based - A return value `0` means that there is one item stored in the list.
Please remember that a return value of `-1` means that there are no items stored in the list.

\return     int     [int]       Zero-based number of Items in the List.
*/
template 
int AList::Count(){
    return count;
}

//! Clears the content of the List.
/*!

\return     void    [void]
*/
template 
void AList::Clr(){
    PrivateListItem* pLItem = first;
    while(count > -1){
        PrivateListItem* tbdListItem = pLItem;
        pLItem = pLItem->nxt;
        delete tbdListItem;
        count--;
    }
}

我的错误:

PWM.cpp.o: In function `__static_initialization_and_destruction_0':
PWM.cpp:4: undefined reference to `AList::AList()'
PWM.cpp:4: undefined reference to `AList::~AList()'

再说一次,AList是有用的而不是我的,但我把它作为参考.我已经查看了有关此错误的所有其他问题,但它们似乎都不适用于我的问题.我知道这是一个包含大量代码的复杂问题,但是感谢您查看并帮助我.

1 个回答
  • 在任何正常的C++项目中,我建议将整个AList文件放在其头文件中; 即拿取内容AList.cpp并坚持到最后AList.h.

    原因是许多C++编译器无法处理与声明分离的模板定义.它可能不适用于Arduino IDE使用的编译器(我还是很新的),但它值得尝试.

    我要做的另一个建议就是放入#include "AList.h"你的PWM.h标题.严格来说,由于包含顺序project.h,它不应该是必要的,但依靠它并不总是好的.

    2023-02-13 15:14 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有