Error LNK2001 unresolved external symbol "private: static class std::list<long,class std::allocator<long> > PersonalIdArchive::personalIdArchive" (?personalIdArchive@PersonalIdArchive@@0V?$list@JV?$allocator@J@std@@@std@@A)
I've been given a school assignment and a part of it is creating a Person class, derived classes Male and Female(the objects should contain a personalid, first name, last name, gender and date), as well as a PersonalIdArchive class which stores id of every object of Male,Female. The archive class shouldn't generate objects so i put the constructor in private, i also put 'static' for all fields and methods. I get the above error and i did some research on it but i have no idea how to fix it.I guess my take on the archive class might be a complete failure but i hope someone can help. Here's the code so far(I removed some parts of the code that are not relevant to the problem):
class PersonalIdArchive {
public:
//these methods were given in the assignment:
static void addRecord(long id);
static list <long>getRecords();
static long nextId();
static bool containsId(long id);
private:
//these fields were given in the assignment, however, i don't know if personalIdArchive should be a list, i assumed it should because of the getReords() method
static long nextPersonalId;
static list <long> personalIdArchive;
PersonalIdArchive();
PersonalIdArchive(const PersonalIdArchive &id);
};
#endif
void PersonalIdArchive::addRecord(long id) {
//PersonalIdArchive::personalIdArchive.push_back(id);
//if(containsId(id)){
//exception////////////////////// i will add this myself
// }
if (PersonalIdArchive::nextPersonalId == id) PersonalIdArchive::nextPersonalId++;
}
long PersonalIdArchive::nextId() {
return PersonalIdArchive::nextPersonalId;
}
bool PersonalIdArchive::containsId(long id) {
bool containsid = false;
for (list<long>::iterator it = PersonalIdArchive::personalIdArchive.begin(); it != PersonalIdArchive::personalIdArchive.end(); ++it) {
if (id == *it) {
containsid = true;
return containsid;
}
}
return containsid;
}
// and this is the Person class, which needs to call the PersonalIdArchive
Person.h
#ifndef _PERSON_H_
#define _PERSON_H_
#include "Date.h"// not relevant to the problem
#include "PersonalIdArchive.h"
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
Person(string first, string last, Date date, long id);
virtual char gender() = 0;
friend class PersonalIdArchive;//not sure if this should even be here, it wasn't mentioned in the assignment
protected:
long personalId;
};
class Male : public Person {
public:
Male(string first, string last, Date date, long id);
};
class Female : public Person {
public:
Female(string first, string last, Date date, long id);
};
#endif
Person.cpp
#include "Person.h"
Person::Person(string first, string last, Date d, long id) : dateOfBirth(d){
firstName = first;
lastName = last;
dateOfBirth = d;
personalId = id;
bool contains = PersonalIdArchive::containsId(id);
if (contains) {
this->personalId = PersonalIdArchive::nextId();
PersonalIdArchive::addRecord(this->personalId);
}
else {
PersonalIdArchive::addRecord(this->personalId);
}
}
Male::Male(string first, string last, Date date, long id) :Person(first, last, date, id) {
}
Female::Female(string first, string last, Date date, long id) : Person(first, last, date, id) {
You need to put the static variable personalIdArchive into the .cpp file like you did with nextPersonalId. Actually any static variable needs it representation in the .cpp.