0

I have a header & cpp file located in Arduino\libraries\Parentfolder\Header&Cpp. Arduino IDE recognizes my lib (the class is color coded & Sketch->include library->MyLibrary shows in the dropdown menu and #includes properly).

Problem: Arduino IDE gives undefined reference to MyClass . . . error in my sketch for every reference of the class including function calls. The function fully in other IDEs as header/cpps so I don't think pasting the cody is necessary -- what could I be missing? I've read a million Stack Exchange posts with similar error/watched a million YouTube guides on writing Arduino libs and I can't seem to find the differentiating factors that would make my code not work for Arduino IDE.

tl;dr what are the reasons that a working c++ library (self-made) would not work with Arduino IDE?

/e

Queue.h
#ifndef Queue_h
#define Queue_h

#include "Arduino.h"

template <typename T>
class Queue
{
  public:
    typedef struct node
    {
      T data;
      node * next = nullptr;
    } * nodePtr;

    nodePtr head; // = nullptr;
    nodePtr tail; // = nullptr;
    nodePtr curr; // = nullptr;
    nodePtr temp; // = nullptr;

    unsigned int counter;

    Queue();
    void push(T);
    T& peek();
    T pop();
    int size();
    bool isEmpty();
    void clear();
};

#endif

Queue.cpp

#include "Arduino.h"
#include "Queue.h"

template <typename T>
Queue<T>::Queue()
{
    nodePtr head = nullptr;
    nodePtr tail = nullptr;
    nodePtr curr = nullptr;
    nodePtr temp = nullptr;

    counter = 0;
}

template <typename T>
void Queue<T>::push(T data)
{
  if (head == nullptr)
  {
    head = new node;
    head->data = data;
    tail = head;
  }
  else
  {
    temp = head;
    head = new node;
    head->data = data;
    head->next = temp;    
  }
  counter ++;
}

template <typename T>
T& Queue<T>::peek()
{
  if (head == nullptr)
  {
    return 0;
  }
  return head->data;

}

template <typename T>
T Queue<T>::pop()
{
  // Empty
  if (head == nullptr)
  {
    return 0;
  }

  // Head = tail
  if (head == tail)
  {
    T data = head->data;
    delete head;
    return data;
  }

  // Size > 1
  curr = head;
  while (curr->next != tail)
  {
    curr = curr->next;
  }
  T data = curr->next->data;  
  tail = curr;
  curr = curr->next;
  delete curr;
  counter --;
  return data;
}

template <typename T>
int Queue<T>::size()
{
  return counter;
}

template <typename T>
bool Queue<T>::isEmpty()
{
  if (counter ==0)
  {
    return true;
  }
  return false;
}

template <typename T>
void Queue<T>::clear()
{
  while (head->data != nullptr)
  {
    temp = head->next;
    delete head;
    head = temp;    
  }
  if (head == nullptr)
  {
    return;
  }
  delete head;
  return;
}

.ino

. . .
Queue<float> avgQueue;
. . .

error

. . . In function `__static_initialization_and_destruction_0':
. . . undefined reference to `Queue<float>::Queue()'

SOLVED:

Combining the .cpp contents in the .h and removing the .cpp worked.

  • Please post a Minimal, Complete, and Verifiable example. Do you have a single sketch file, or have you broken your code into multiple files (eg. cpp/h files)? – Nick Gammon Jan 11 '18 at 23:03
  • The header and cpp are in the forementioned subfolder which is inside of the Arduino library folder. The errors come from a simple sketch meant only to test the (already-working-in-other-IDEs) library. – seiqooq Jan 11 '18 at 23:10
  • one thing that jumps out is the ampersand & in the path ... maybe the compiler does not like it – jsotola Jan 12 '18 at 0:08
  • That's not the real path, that's just me saying that both the header&cpp are there. – seiqooq Jan 12 '18 at 1:43
-1

SOLVED:

Putting the .cpp contents in the .h and removing the .cpp from the lib folder entirely worked.

  • Yes, tempates are instantiated on demand so you have to have complete class inside of header. However I wonder how the pop works if you have the last element. Your head wasn't set to the nullptr - and delete can't do that for you. – KIIV Jan 12 '18 at 6:17
  • Your code doesn't totally prove the problem, however libraries are not included unless you include them in the .ino file which might be your underlying issue. – Nick Gammon Jan 12 '18 at 6:23
  • This, for example, is not complete code: . . . Queue<float> avgQueue; . . . – Nick Gammon Jan 12 '18 at 6:23
  • @NickGammon fortunately I'm not that much of a noob! The lib was included and everything is now functioning well as a result of the above workaround – seiqooq Jan 13 '18 at 4:14
  • I'm glad it is working, however I'm not sure what you did exactly (that didn't work) and what you did (that did work). However this looks relevant. – Nick Gammon Jan 13 '18 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.