Hallo,
I have developed a simple c++ template with intention to use it in my further projects. I have placed the .cpp, .h and .ino files into one program folder. The program folder is located in Arduino folder. When I try to compile it under Arduino IDE, I obtain undefined reference errors. Can you help me to fix this issue? Please see the details below.
The content of the program folder is:
\test_dataset.ino
\dataset.h
\dataset.cpp
The program folder is located just below the Arduino folder:
Arduino\test_dataset
The content of the the files is:
test_dataset.ino:
#include "dataset.h"
void setup() {
// put your setup code here, to run once:
Dataset<Dataset<int>> daty;
int j;
int k;
Serial.begin(9600);
daty.nastavVelikost(3);
daty[0].nastavVelikost(2);
daty[1].nastavVelikost(2);
daty[2].nastavVelikost(2);
for(j=0;j<3;++j){
for(k=0;k<2;++k){
daty[j][k] = 2*j + k;
Serial.print("daty[");
Serial.print(j);
Serial.print("]");
Serial.print("[");
Serial.print(k);
Serial.print("] = ");
Serial.println(daty[j][k]);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
}
dataset.h:
#ifndef _DATASET_
#define _DATASET_
#include <Arduino.h>
template <class T>
class Dataset {
private:
// pocet prvku pole
unsigned int N;
public:
Dataset();
Dataset( unsigned int i_N);
~Dataset();
T * pole;
// pocet nenulovych prvku pole - volitelna promenna,
// ktera muze byt dale pouzita ve vypoctech napr. FFT
unsigned int Nfyz;
// zapisovani prvku Datasetu a cteni z nich
T & operator[](unsigned int i_n);
// prepisovani pole jinym polem
Dataset<T> & operator=(Dataset & i_Dts);
//nastavovani velikosti pole vcetne kopie starych prvku
int nastavVelikost(unsigned int i_N);
unsigned int velikost();
T minVal();
T maxVal();
};
#endif
dataset.cpp:
#include "dataset.h"
template <class T>
Dataset<T>::Dataset(){
pole = NULL;
N = 0;
Nfyz = 0;
}
template <class T>
Dataset<T>::Dataset(unsigned int i_N){
if(i_N > 0){
pole = new T [i_N];
memset( pole, 0, i_N*sizeof(T));
N = i_N;
Nfyz = 0;
}
else{
pole = NULL;
N = 0;
Nfyz = 0;
Serial.println("Dataset: v konstruktoru datasetu je zaporny argument");
}
}
template <class T>
Dataset<T>::~Dataset(){
if(pole != NULL){
delete [] pole;
}
}
template <class T>
T & Dataset<T>::operator[](unsigned int i_n){
bool horniMez;
horniMez = (i_n < N);
if(pole != NULL && horniMez){
return pole[i_n];
}
else{
Serial.println("Dataset:operator[] index je mimo rozsah pole");
}
}
template <class T>
Dataset<T> & Dataset<T>::operator=(Dataset<T> & i_Dts){
int k;
int velikostPole;
bool vytvorPole = false;
velikostPole = i_Dts.N;
// pole je jiz vytvorene
if(pole != NULL){
//vstupni pole nema stejnou velikost
if(N != velikostPole){
delete [] pole;
pole = NULL;
vytvorPole = true;
}
// vstupni pole je nenulove
if(velikostPole > 0){
// pole nemaji stejnou velikost
if(vytvorPole){
N = velikostPole;
pole = new T [velikostPole];
}
memcpy( pole, i_Dts.pole, N*sizeof(T));
Nfyz = i_Dts.Nfyz;
}
// vstupni pole je nulove
else{
N = 0;
Nfyz = 0;
}
}
//pole jeste neni vytvorene
else{
if(velikostPole > 0){
pole = new T [velikostPole];
N = velikostPole;
memcpy( pole, i_Dts.pole, N*sizeof(T));
Nfyz = i_Dts.Nfyz;
}
// vstupni pole je nulove
else{
//pole = NULL;
N = 0;
Nfyz = 0;
}
}
return *this;
}
template <class T>
int Dataset<T>::nastavVelikost(unsigned int i_N){
bool zkopirujStaraData = false;
T * buff = new T [N];
if(i_N == 0){
if(pole != NULL){
delete [] pole;
pole = NULL;
N = 0;
Nfyz = 0;
}
else{
N = 0;
Nfyz = 0;
}
return 0;
}
else{
if(pole != NULL){
memcpy( buff, pole, N*sizeof(T));
zkopirujStaraData = true;
delete [] pole;
}
pole = new T [i_N];
memset( pole, 0, i_N*sizeof(T));
if(zkopirujStaraData){
memcpy( pole, buff, min(i_N,N)*sizeof(T));
}
}
N = i_N;
delete [] buff;
}
template <class T>
T Dataset<T>::minVal (){
int k;
T minVal;
if(N == 0){
return T(0);
}
minVal = (*this)[0];
for(k=0;k<N;++k){
if((*this)[k] < minVal){
minVal = (*this)[k];
}
}
return minVal;
}
template <class T>
T Dataset<T>::maxVal (){
int k;
T maxVal;
if(N == 0){
return T(0);
}
maxVal = (*this)[0];
for(k=0;k<N;++k){
if((*this)[k] > maxVal){
maxVal = (*this)[k];
}
}
return maxVal;
}
template <class T>
unsigned int Dataset<T>::velikost(){
return N;
}
The Error message, that I obain after compilation in Arduino IDE:
Arduino: 1.8.8 (Linux), Board: "ESP32 Dev Module, Disabled, Huge APP (3MB No OTA/1MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"
sketch/test_dataset.ino.cpp.o:(.literal._Z5setupv+0x20): undefined reference to `Dataset<Dataset<int> >::Dataset()'
sketch/test_dataset.ino.cpp.o:(.literal._Z5setupv+0x24): undefined reference to `Dataset<Dataset<int> >::nastavVelikost(unsigned int)'
sketch/test_dataset.ino.cpp.o:(.literal._Z5setupv+0x28): undefined reference to `Dataset<Dataset<int> >::operator[](unsigned int)'
sketch/test_dataset.ino.cpp.o:(.literal._Z5setupv+0x2c): undefined reference to `Dataset<int>::nastavVelikost(unsigned int)'
sketch/test_dataset.ino.cpp.o:(.literal._Z5setupv+0x30): undefined reference to `Dataset<int>::operator[](unsigned int)'
sketch/test_dataset.ino.cpp.o:(.literal._Z5setupv+0x34): undefined reference to `Dataset<Dataset<int> >::~Dataset()'
sketch/test_dataset.ino.cpp.o: In function `setup()':
/home/dalibor/Arduino/test_dataset/test_dataset.ino:5: undefined reference to `Dataset<Dataset<int> >::Dataset()'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:12: undefined reference to `Dataset<Dataset<int> >::nastavVelikost(unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:14: undefined reference to `Dataset<Dataset<int> >::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:14: undefined reference to `Dataset<int>::nastavVelikost(unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:15: undefined reference to `Dataset<Dataset<int> >::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:15: undefined reference to `Dataset<int>::nastavVelikost(unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:16: undefined reference to `Dataset<Dataset<int> >::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:16: undefined reference to `Dataset<int>::nastavVelikost(unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:20: undefined reference to `Dataset<Dataset<int> >::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:20: undefined reference to `Dataset<int>::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:28: undefined reference to `Dataset<Dataset<int> >::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:28: undefined reference to `Dataset<int>::operator[](unsigned int)'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:5: undefined reference to `Dataset<Dataset<int> >::~Dataset()'
/home/dalibor/Arduino/test_dataset/test_dataset.ino:5: undefined reference to `Dataset<Dataset<int> >::~Dataset()'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.
Invalid library found in /home/dalibor/Arduino/libraries/TFT_eSPI_user_SETUPS: no headers files (.h) found in /home/dalibor/Arduino/libraries/TFT_eSPI_user_SETUPS
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Can anybody give an advice how to fix the "undefined reference" errors?
Thanks.