vendredi 23 mars 2018

Undefined reference to static vector

I have the following method

Ant* Ant::findAntAtPos(int x, int y)  {
    for(int i = 0; i < Ant::ants->size(); i++) {
        Ant *ant = Ant::ants->at(i);
        if (ant->getX() == x && ant->getY() == y) {
            return ant;
        }
    }
    return nullptr;
}

Ant.h contains

#ifndef SIMULATION_ANT_H
#define SIMULATION_ANT_H

#include <cstdlib>
#include <vector>
#include "Organism.h"

class Ant:public Organism {

public:

    static std::vector<Ant*> *ants;

    int turnsAlive = 0;


    static Ant *findAntAtPos(int x, int y);

    void move(char *board, int length);

    void breed(char *board, int length);

    Ant(int xPos, int yPos):Organism(xPos, yPos, 'o') {
        ants->push_back(this);
    }
};

#endif //SIMULATION_ANT_H

When I try to compile it doesn't seem to be able to find the static vector

Undefined symbols for architecture x86_64:
  "Ant::ants", referenced from:
      Ant::findAntAtPos(int, int) in Ant.cpp.o
      Ant::Ant(int, int) in Ant.cpp.o

I have no idea why this wouldn't work. I am fairly new to C++ and have been struggling with this for a few hours through various Google searches and writing test code.

How do I resolve this error?

Thanks

Aucun commentaire:

Enregistrer un commentaire