Print-ABC
Print-ABC

Reputation: 11

C++: Singleton Class Design (Error: unresolved external symbol)

I'm implementing a singleton class design for a C++ project on Visual Studio. It was based on this GitHub code sample. Logic-wise, my code seems correct, but there's an error from my compiler. Does anyone know what's wrong?

I have several singleton classes. Here's a code sample for one of them.

AccountBG.h

#ifndef ACCOUNTBG_H
#define ACCOUNTBG_H

#include "Lecturer.h"
#include "Student.h"
#include "Type.h"

class AccountBG
{
public:
   static AccountBG* getInstance();
   // GET Methods
   Lecturer* getLecturer();
   Student* getStudent();
   // SET Methods
   void setLecturer(int, string);
   void setStudent(int, string);
private:
   // Singleton class instance
   static AccountBG* instance;
   AccountBG(); // Private constructor to prevent instancing.
   Lecturer *lecturer;
   Student *student;
};

#endif // !ACCOUNTBG_H

AccountBG.cpp

#include "AccountBG.h"
// Null, because instance will be initialized on demand.
AccountBG* AccountBG::instance = 0;

AccountBG* AccountBG::getInstance() {
    if (instance == 0) {
        instance = new AccountBG();
    }
    return instance;
}

// GET Methods
Lecturer* AccountBG::getLecturer() {
    return this->lecturer;
}

Student* AccountBG::getStudent() {
    return this->student;
}

// SET Methods
void AccountBG::setLecturer(int id, string username) {
    this->lecturer = new Lecturer();
    this->lecturer->setID(id);
    this->lecturer->setUsername(username);
    this->lecturer->setType("Lecturer");
}

void AccountBG::setStudent(int id, string username) {
    this->student = new Student();
    this->student->setID(id);
    this->student->setUsername(username);
    this->student->setType("Student");
}

Screenshot of Compiler Errors

Errors as seen on VS2015

Upvotes: 0

Views: 275

Answers (1)

zhm
zhm

Reputation: 3641

unresolved external symbol usually caused by calling a declared, but un-implemented function.

In your case, you don't implement constructor for AccountBG, only declared.

Possible same problem for other functions:

MarkingData::instance()
Quiz::Quiz()
QuizTracker::QuizTracker()

Add implementation to these functions may solve your problem.

Upvotes: 1

Related Questions