OOP - using vectors in classes

Being accustomed Java, I’ve downloaded the StandardC++ library for Arduino IDE so I can access as many utilities as I can when designing classes, notably containers for objects (I was looking for an equivalent of arraylists)

I want to declare a public static vector where upon instantiating an object, the object’s identification would be added to the vector (pretty straightforward stuff):

class Cow{
public:
static vector herd;
int RFID;
Cow(int rfid){
RFID = rfid;
herd.push_back(rfid);

}
};
Cow cow1(173);

But this does not compile and results in exit status 1.

So I am at a bit of a loss here; I’ve entered the stage where I’m not really reasoning why things don’t work and just mucking about.

And THIS does work:

vector herd;
class Cow{
public:
int RFID;
Cow(int rfid){
RFID = rfid;

}
};
Cow cow1(173);

void setup(void) {
herd.push_back(cow1.RFID);
}

So when I declare the vector outside of the class, and use the push_back function in the setup (I’ve tried to use push_back outside of the setup and that does not work either), it’s fine. But alas, this is no longer OOP, which was is my ultimate aim.

Any guidance or ray of insight would be dearly, dearly appreciated.
I am appealing to all you generous arduino aficionados basked in wisdom and erudition; and I thank you beforehand!

uqion:
But this does not compile and results in exit status 1.

The complete compiler error message might be helpful.

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\luwinna\AppData\Local\Temp\ccZmnXJ9.ltrans0.ltrans.o: In function `GLOBAL_sub_I_cow1':

ccZmnXJ9.ltrans0.o:(.text.startup+0x108): undefined reference to `Cow::herd'

ccZmnXJ9.ltrans0.o:(.text.startup+0x10c): undefined reference to `Cow::herd'

ccZmnXJ9.ltrans0.o:(.text.startup+0x11c): undefined reference to `Cow::herd'

Okay number one:

I didn't know there was a more comprehensive compiler message.

And two:

I should probably initialize the static variable. (That's the problem, yeah?)

uqion:
I should probably initialize the static variable. (That’s the problem, yeah?)

yep.

#include "ArduinoSTL.h"
#include <vector>

class Test {
  public:
    Test(int someVal) : val(someVal) {
      Instances.push_back(this);
    }
    int val;
    static std::vector<Test*> Instances;
};

std::vector<Test*> Test::Instances;

Test test[] = {1,2,3,4,5,6};


void setup() 
{
  Serial.begin(9600);
  for (auto i : Test::Instances) {
    Serial.println(i->val);
  }
}

void loop() 
{
  
}

Ermahgerd. Thanks so much. xoxoxo

Watch out for those container classes. You're not writing code for a PC anymore. You need to be very careful about memory usage. You don't have any garbage collection to help out. Dynamic allocation can get you into a lot of trouble. On the micros it is often easier to just do things the hard way.