c++classooppointerssingleton-type

What is the problem about static pointer?


I've just started learning OOP. I want to create a LinkList of BookList and use Singleton design pattern to create a unique object "library" to manage this BookList, but there is problem when initializing BookList.

class Library {
public:
static Library* library;
static BookList* pHead;
static int i;
static int getNewID(BookType typ) {
    return i++;
}
static Library* getLibrary();
static Library* getLibrary(BookList*);
static BookList* getNull(BookList*);
private:
Library(BookList* a); 
};
Library* Library::getLibrary() {
if (library == NULL) {
        BookList* a = new BookList;
        library = new Library(a);
    }
    return library;
}
Library* Library::getLibrary(BookList* a) {
    if (library == NULL) {
        library = new Library(a);
    }
    return library;
}
Library::Library(BookList* a) {
    BookList* p = pHead;
    while (1) {
        if (p->getNext() == NULL) {  //I got error
        p->setNext(a);
        break;
        }
        p = p->getNext();
    }
}

And i got an error here:

Undefined symbols for architecture x86_64: "Library::pHead", referenced from: Library::Library(BookList*) in test-9c557d.o ld: symbol(s) not found for architecture x86_64

Did i forgot something?


Solution

  • Seems like you didn't define pHead - you just declare it. Try to add the following code in your implementation file (.cpp / .cxx):

    BookList* Library::pHead = new BookList();