0

I have these data members static because Array will be an abstract class that will have have multiple classes that will inherit it. I want to be able to load data from a text file into my primaryCollections array.

Keep getting an error when I try to compile my code:

/usr/bin/ld: Array.o: warning: relocation against `_ZN5Array17primaryCollectionE' in read-only section `.text'
/usr/bin/ld: Array.o: in function `Array::loadData(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Array.cc:(.text+0x240): undefined reference to `Array::primaryCollection'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make: *** [Makefile:6: p1] Error 1

Array.cc:

//loads data from text file into a list of record objects
void Array::loadData(string f){

  int year, employed, graduates;
  string province, degree, gender;

  ifstream file(f, ios::in);

  if (!file) {
    cout << "Error: could not open file" << endl;
    exit(1);
  }

  while ( file >> year >> province >> degree >> gender>> employed>> graduates ) {

    Record *record = new Record(year,province,degree,gender,employed,graduates);
    primaryCollection.push_back(record);

  }

  
}

Array.h:

class Array {

  
  public:

    static void loadData(string);

    
  private:
    string name;
    static vector<Record*> primaryCollection;

  

};
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
MajorMajor
  • 1
  • 1
  • 1

1 Answers1

1

With C++17 you can use the inline modifier:

static inline vector<Record*> primaryCollection;

This will work as expected.

If you are interested in details see: https://en.cppreference.com/w/cpp/language/inline

F K
  • 315
  • 2
  • 9