::AList()" Arduino C++-6ren"> ::AList()" Arduino C++-我正在使用 AList library为 Arduino 创建一个双向链表,但我在定义它时遇到编译错误。该库不是我写的,其他人已经成功使用它,所以我认为这不是问题,而是我的代码有问题。我也在使用 Ar-6ren">
gpt4 book ai didi

c++ - 编译错误 : "undefined reference to AList::AList()" Arduino C++

转载 作者:行者123 更新时间:2023-11-30 00:51:56 25 4
gpt4 key购买 nike

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

 #include "project.h"
//other stuff

项目.h:

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

脉宽调制.h:

#ifndef PWM_H
#define PWM_H

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

#endif

脉宽调制.cpp:

#include "project.h"

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

AList.h:

#ifndef ALIST_H
#define ALIST_H

template <typename ListItem>
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<ListItem> A new instance of an AList.
*/
template <typename ListItem>
AList<ListItem>::AList(){
count = -1;
}
//! Destroys the instance of AList<ListItem>.
/*!
The AList<ListItem>::Clr() is called in order to free memory which
was previously occupied by the dynamically allocated list items.
\sa Clr();
*/
template <typename ListItem>
AList<ListItem>::~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 <typename ListItem>
void AList<ListItem>::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 <typename ListItem>
void AList<ListItem>::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 <typename ListItem>
ListItem AList<ListItem>::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 <typename ListItem>
ListItem AList<ListItem>::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<ListItem>::First().

\return ListItem [ListItem] The first ListItem in the list.
\sa First(), Count()
*/
template <typename ListItem>
ListItem AList<ListItem>::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 <typename ListItem>
int AList<ListItem>::Count(){
return count;
}

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

\return void [void]
*/
template <typename ListItem>
void AList<ListItem>::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<Pwm>::AList()'
PWM.cpp:4: undefined reference to `AList<Pwm>::~AList()'

同样,众所周知,AList 可以工作,而不是我的,但我将其包含在内以供引用。我已经查看了有关此错误的所有其他问题,但似乎没有一个适用于我的问题。我知道这是一个包含大量代码的复杂问题,但感谢您查看它并帮助我解决问题。

最佳答案

在任何普通的 C++ 项目中,我建议将整个 AList 放在它的头文件中;即获取 AList.cpp 的内容并粘贴在 AList.h 的末尾。

原因是许多 C++ 编译器无法处理像这样将模板定义与声明分开的情况。它可能不适用于 Arduino IDE 使用的编译器(我对它还是很陌生),但值得一试。

我要提出的另一个建议是将 #include "AList.h" 放在您的 PWM.h header 中。严格来说,由于 project.h 中的包含顺序,它不是必需的,但依赖它并不总是好的。

关于c++ - 编译错误 : "undefined reference to AList<Pwm>::AList()" Arduino C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com