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.
&
in the path ... maybe the compiler does not like it