This is my code structure:
Flashtraining.h
#ifndef Flashtraining_h
#define Flashtraining_h
#include "Enhanced_SPIFlash_Marzogh\\SPIFlash.h"
class Flashtraining
{
static SPIFlash myflash; //Should it be declared outside private/public? Does it matter?
public:
bool start_new_training();
bool stop_training();
/*Some more methods*/
private:
bool _Check_or_Initialize();
};
Flashtraining.cpp
#include "Flashtraining.h"
SPIFlash Flashtraining::myflash;
bool Flashtraining::start_new_training(){
/*Do smth. with myflash object*/
}
bool Flashtraining::stop_training()
{
/*Also do smth. with myflash object*/
}
main.cpp
#include "Flashtraining.h"
Flashtraining training; //Create object of class Flashtraining
training.start_new_training(); //Ignoring the main funcion, return values etc.
SPIFlash
has no constructor and only non-static members. If you need this file too, comment and I will provide it, but I am sure it is not the problem here.
My problem: When executing this code, I get
C:\Users\--\Flashtraining.cpp.o: In function `Flashtraining::_Check_or_Initialize()':
C:\Users\--\Flashtraining.cpp:265: undefined reference to `SPIFlash::begin(unsigned char, unsigned long)'
C:\Users\--\Flashtraining.cpp:268: undefined reference to `SPIFlash::getJEDECID()'
Note: _Check_or_Initialize()
is one of the functions declared in the Flashtraining.cpp file. It is declared just like the other ones I have provided.
The functions in SPIFlash
are declared non-static and implemented correctly.
What I don't understand: Because I use the static keyword, I can just declare an object(myflash
of type SPIFlash
) in the header file (Flashtraining.h
) without initializing it, right? This also prevents redefinitions.
Because of the One definition rule in c++, I initialize the variable once in Flashraining.cpp
, where it is used. I then should be able to use (.) notation to access non-static members (the functions, like myflash.start_training()
), or not?
Why is this giving me an undefined reference and how can I fix it?
I looked at this post and think that I changed my code accordingly, but it doesn't work.
Additional information: This is .ino code, which is based on c++. Everything written in c++ works normally. Using -std=gnu++11
.