首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译错误:“对AList<Pwm>::AList()的未定义引用”Arduino C++

编译错误:“对AList<Pwm>::AList()的未定义引用”Arduino C++
EN

Stack Overflow用户
提问于 2013-12-02 01:46:36
回答 2查看 1.4K关注 0票数 1

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

代码语言:javascript
复制
 #include "project.h"
 //other stuff

H项目:

代码语言:javascript
复制
#include "Arduino.h"
#include "AList.h"
#include "PWM.h"
//other stuff

PWM.h:

代码语言:javascript
复制
#ifndef PWM_H
#define PWM_H

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

#endif

PWM.cpp:

代码语言:javascript
复制
#include "project.h"

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

阿利斯特h:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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--;
    }
}

我的错误:

代码语言:javascript
复制
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是工作的,而不是我的,但我把它作为参考。我看过关于这个错误的所有其他问题,似乎没有一个适用于我的问题。我知道这是一个复杂的问题,有很多代码,但谢谢你看了它,并帮助我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-02 15:13:22

对于任何普通的C++项目,我都建议将整个AList放在它的头文件中;也就是说,将AList.cpp的内容放在AList.h的末尾。

原因是许多C++编译器无法处理模板定义与声明分离的问题。它可能不适用于Arduino IDE使用的编译器(我对它仍然很陌生),但值得一试。

我要提出的另一个建议是将#include "AList.h"放在PWM.h头中。严格地说,它不应该是必要的,因为在project.h中包含顺序,但是依赖它并不总是好的。

票数 6
EN

Stack Overflow用户

发布于 2014-06-17 15:40:05

我在这上面绊倒了好几个小时。第一个答案工作得很好,但是如果您想在构建库时编译库,那么还需要一点改进。

我的设置(供您参考)

  • 我不使用Arduino的库文件夹,而是引用如下所示: #include "../includes/MyClassName/src/MyClassName.h"
  • 崇高的文本3测试版与Stino插件(强烈推荐-我很喜欢它!)
  • Arduino IDE版本: 1.5.6 beta

好了,接下来是密码。这是MyClassName.h:

代码语言:javascript
复制
#ifndef MyClassName_h //If MyClassName_h "Not" DEFined
#define MyClassName_h //Then define MyClassName_h
class MyClassName{
  public:
    void get_computer_msg(boolean echo_cmd = false);
    String computer_msg;
    char first_char;
};
#endif //End if

#include "MyClassName.cpp" //Same as first answer recommends

这是MyClassName.cpp

代码语言:javascript
复制
#ifndef MyClassName_cpp //the _cpp is just to delineate between the two files  
#define MyClassName_cpp
#include "MyClassName.h"
void MyClassName::get_computer_msg(boolean echo_cmd) { //Receive message from computer
  int index = 0;
  String msg = "";
  String echo_msg = "";
  if(Serial.available()) {
    while(Serial.available()) {
      char cChar = Serial.read();
      msg += cChar;
      echo_msg += cChar;
      index++;
    }
    if(echo_cmd) Serial.println("Computer sent " + echo_msg + "+CRLF");
    first_char = msg.charAt(0);
    computer_msg = msg;
  }
}
#endif

这样做的好处是您可以加载MyClassName.cpp,修改它,然后立即编译。如果你有任何错误,你就会知道的。此外,您也不必在MyClassName.h中更新任何内容,因为其中包含了MyClassName.cpp和#ifndef的“不进行双重声明”。

对于使用这种方法,我唯一不确定的是,编译时间仍然可能会因为在MyClassName.cpp文件中包含.h而受到阻碍。这是首先拥有独立文件的主要原因。也许C++的专家可以对此发表评论?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20319607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档