C++:单例类设计(错误:未解析的外部符号)
C++: Singleton Class Design (Error: unresolved external symbol)
我正在Visual Studio上为一个C++项目实现一个单例类设计。它基于此 GitHub 代码示例。从逻辑上讲,我的代码似乎是正确的,但是我的编译器出现了错误。有谁知道出了什么问题?
我有几个单例类。下面是其中一个代码示例。
帐户BG.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
帐户BG
.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");
}
编译器错误的屏幕截图
VS2015 上出现的错误
未解析的外部符号,通常由调用已声明但未实现的函数引起。
在您的情况下,您没有实现 AccountBG
的构造函数,只是声明。
其他功能可能出现相同的问题:
MarkingData::instance()
Quiz::Quiz()
QuizTracker::QuizTracker()
向这些函数添加实现可能会解决您的问题。
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector