0

I am getting this error:

g++ -c location.cpp
g++ -c place.cpp
g++ -c popularplace.cpp
g++ -c person.cpp
g++ -o main main.cpp *.o
Undefined symbols for architecture x86_64:
  "sim::PopularPlace::arraycounter", referenced from:
      sim::PopularPlace::PopularPlace() in popularplace.o
      sim::PopularPlace::PopularPlace(double, double) in popularplace.o
  "sim::PopularPlace::places", referenced from:
      sim::Person::action() in person.o
      sim::PopularPlace::PopularPlace() in popularplace.o
      sim::PopularPlace::PopularPlace(double, double) in popularplace.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

When building this makefile:

main: main.cpp simulation.h location.o place.o popularplace.o person.o
    g++ -o main main.cpp *.o

location.o: location.cpp location.hpp simulation.h
    g++ -c location.cpp

place.o: place.hpp place.cpp
    g++ -c place.cpp

popularplace.o: place.hpp place.cpp simulation.h popularplace.hpp popularplace.cpp
    g++ -c popularplace.cpp

person.o: person.cpp person.hpp location.hpp simulation.h place.hpp place.cpp location.cpp popularplace.o
    g++ -c person.cpp
clean:
    rm *.o

I can't find the error. Here is the popularplace.cpp file:

#include "popularplace.hpp"
#include "simulation.h"

namespace sim{
  PopularPlace::PopularPlace()
    :open(true){
      places[PopularPlace::arraycounter] = this;
      PopularPlace::arraycounter++;
    }

  PopularPlace::PopularPlace(double x, double y)
    :Place(x, y), open(true){
      places[PopularPlace::arraycounter] = this;
      PopularPlace::arraycounter++;
    }

  bool PopularPlace::isOpen() const{
    return open;
  }
  void PopularPlace::close(int numOfCasesToReopen){
    open = false;
  }
  bool PopularPlace::reopen(int activeCases){
    if(activeCases <= numOfCasesToReopen){
      open = true;
      return true;
    } else{
      return false;
    }
  }
}

and here's the popularplace.hpp file:

#ifndef POPULAR_PLACE_HPP
#define POPULAR_PLACE_HPP
#include "simulation.h"
#include "place.hpp"

namespace sim{
  class PopularPlace : public Place{
  private:
    bool open;
    int numOfCasesToReopen;
    static int arraycounter;
  public:
    static PopularPlace* places[POPULAR_PLACES];
    PopularPlace();
    PopularPlace(double x, double y);
    bool isOpen() const;
    void close(int numOfCasesToReopen);
    bool reopen(int activeCases);
  };
}
#endif

If you need me to provide additional information/code, I'd be happy to do so. Thanks.

Serket
  • 3,785
  • 3
  • 14
  • 45

1 Answers1

1

Static variables need a definition; they are declared in the header but they should also be defined in the cpp file. This is also where you'd initialize them. For example:

int PopularPlace::arraycounter{0};
PopularPlace* PopularPlace::places[POPULAR_PLACES] = {};

However, you might consider using an std::vector<std::shared_ptr<PopularPlace>> instead of a manually-managed array.

cdhowie
  • 158,093
  • 24
  • 286
  • 300