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

时间:2013-12-02 01:46:36

标签: c++ arduino

我正在使用AList library为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<Pwm> pwms;
  static int numPwms;
  //other stuff
};

#endif

PWM.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已知工作而不是我的,但我将其包括在内以供参考。我已经查看了有关此错误的所有其他问题,但它们似乎都不适用于我的问题。我知道这是一个包含大量代码的复杂问题,但是感谢您查看并帮助我。

2 个答案:

答案 0 :(得分:6)

在任何普通的C ++项目中,我建议将整个AList放在其头文件中;即取AList.cpp的内容并改为AList.h的结尾。

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

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

答案 1 :(得分:0)

我偶然发现了几个小时。 第一个答案效果很好,但是如果您想在构建库时编译库,可能会有一些不足之处。

我的设置(供参考)

  • 我没有使用Arduino的库文件夹,而是像这样引用:

    #include "../includes/MyClassName/src/MyClassName.h"

  • 使用Stino插件的Sublime Text 3测试版(强烈推荐 - 我非常喜欢!)

  • Arduino IDE版本:1.5.6 beta

好的,现在转到代码。这是MyClassName.h:

#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

#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保持双重声明。

我唯一不确定使用此方法的方法是,在.h文件中包含MyClassName.cpp可能仍会妨碍编译时间。这是首先使用单独文件的主要原因。也许C ++专家可以对此发表评论吗?