I am making a program for arduino. I am using avr-g+ 4.9.2 with STL from here.
I have a class Cocktail. I want all objects of type Cocktail to be able to access a vector of pointers. These pointers point to objects of type Alcohol. Seeing as this vector of pointers is the same for each instance of Cocktail, I wanted to make it static. But then my program fails to compile if I make them static. Here is the code:
Ineb.hpp
class Alcohol
{
private:
float flow_rate_;
Pump * which_pump_;
public:
std::string this_alcohol_;
Alcohol(std::string this_alcohol, float flow_rate);
Alcohol(std::string this_alcohol, float flow_rate, Pump which_pump);
float HowLong(float percentage_of_drink, uint8_t drink_size); //How long in seconds the pump should be on
void ChangeByteToRegister(uint8_t& byte_to_register);
};
class Cocktail
{
private:
bool order_matter_;
uint8_t byte_to_register_;
static std::vector<Alcohol*> alcohol_directory_;
public:
static void test(Alcohol *ba) {alcohol_directory_.push_back(ba);} //STATIC KEYWORD HERE
Cocktail(bool ordr_matter);
std::vector<std::string> GetIngredients(const uint8_t& num_ingredients, PGM_P& string_table);
uint8_t GetByteToRegister();
void MakeDrink(const uint8_t& num_ingredients, PGM_P& string_table);
};
main.cpp
#include "src/Ineb.hpp"
#include "src/Pins.hpp"
#include "ingredients.h"
#include <pnew.cpp>
extern "C" void __cxa_pure_virtual() {
for(;;);
}
int main(void) {
init();
setup();
Ineb::Pump A(1,8);
Ineb::Alcohol Vodka("vodka", 2.5, A);
Ineb::Cocktail::test(&Vodka);
for(;;)
loop();
return 0; // not reached
}
undefined reference to `Ineb::Cocktail::alcohol_directory_'
I'm mainly confused why this compiles when I take away static. What is static doing under the hood??
static
members of classes must be defined outside the class definition.Having the line
in
Cocktail.cpp
will do it.