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.
1 Answer 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. Yourhead
wasn't set to the nullptr - anddelete
can't do that for you.KIIV– KIIV2018年01月12日 06:17:20 +00:00Commented Jan 12, 2018 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.2018年01月12日 06:23:22 +00:00Commented Jan 12, 2018 at 6:23
-
This, for example, is not complete code:
. . . Queue<float> avgQueue; . . .
2018年01月12日 06:23:52 +00:00Commented Jan 12, 2018 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 workaroundseiqooq– seiqooq2018年01月13日 04:14:56 +00:00Commented Jan 13, 2018 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.2018年01月13日 06:23:25 +00:00Commented Jan 13, 2018 at 6:23
&
in the path ... maybe the compiler does not like it