我已经写了一个基本的资源持有人来保存GUI元素,起初它全部在MGUI.h和.cpp文件中,但是我后来才打算在具有孩子的某些GUI元素(例如Windows)中重用该持有人。 ,标签,容器等)。 当所有内容都在MGUI.h和.cpp中时,代码将进行编译,并且尽管我有一些骇人听闻的方法(即我在AddElements中进行的不安全调用)也可以运行。
拆分代码后,我现在出现3个LINK2019错误
1>MGUI.obj : error LNK2019: unresolved external symbol "public: __thiscall MapleStory::MGUI::MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *>::MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *>(void)" (??0?$MElementManager@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVMElement@MGUI@MapleStory@@@MGUI@MapleStory@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'private: static class MapleStory::MGUI::MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *> MapleStory::MGUI::MGUI::elements''(void)" (??__E?elements@MGUI@1MapleStory@@0V?$MElementManager@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVMElement@MGUI@MapleStory@@@12@A@@YAXXZ)
1>MGUI.obj : error LNK2019: unresolved external symbol "public: __thiscall MapleStory::MGUI::MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *>::~MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *>(void)" (??1?$MElementManager@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVMElement@MGUI@MapleStory@@@MGUI@MapleStory@@QAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'private: static class MapleStory::MGUI::MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *> MapleStory::MGUI::MGUI::elements''(void)" (??__F?elements@MGUI@1MapleStory@@0V?$MElementManager@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVMElement@MGUI@MapleStory@@@12@A@@YAXXZ)
1>MGUI.obj : error LNK2019: unresolved external symbol "public: class MapleStory::MGUI::MElement * & __thiscall MapleStory::MGUI::MElementManager<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *>::AddElement(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class MapleStory::MGUI::MElement *)" (?AddElement@?$MElementManager@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVMElement@MGUI@MapleStory@@@MGUI@MapleStory@@QAEAAPAVMElement@23@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAV423@@Z) referenced in function "public: static void __cdecl MapleStory::MGUI::MGUI::CreateUIElements(void)" (?CreateUIElements@MGUI@1MapleStory@@SAXXZ)
我已经检查了include顺序,以确保一切正常,但找不到导致它的原因,还确保了当我编译global.cpp时显示的第一行是global.cpp是项目的一部分上。
无论如何我都不是专业人士,并且可能错过了一些明显的东西,这也是我在这里的第一篇文章,如果我以不好的方式构造它,可以随时询问任何丢失的信息。
MGUI.h-包含所有GUI元素的类-基本上是GUI的根
//MGUI.h
#pragma once
#include "global.h"
#include "MElement.h"
namespace MapleStory
{
namespace MGUI
{
class MGUI
{
...
private:
static MElementManager<std::string, MElement*> elements;
};
}
}
MGUI.cpp
//MGUI.cpp
#include "MGUI.h"
using namespace MapleStory::MGUI;
MElementManager<std::string, MElement*> MGUI::elements;
void MGUI::CreateUIElements()
{
MGUI::elements.AddElement("", (MElement*)0);
//reinterpret_cast<MCheckBox&>(*MGUI::elements.AddElement("MCheckBox1", new MCheckBox())).BindEvent_CheckedChanged(Callback);
}
Global.h-大多数文件中需要的一些有用的类,结构,帮助程序和包含
//global.h
#pragma once
#include <SFML/Graphics.hpp>
#include <Thor/Resources.hpp>
#include <Thor/Graphics.hpp>
#include <Windows.h>
#include <string>
namespace MapleStory
{
namespace MGUI
{
...
template <typename keyType, typename eleType>
class MElementManager
{
public:
MElementManager();
~MElementManager();
typedef typename std::map<keyType, eleType>::iterator iterator;
typedef typename std::map<keyType, eleType>::const_iterator const_iterator;
inline iterator begin() noexcept { return elements.begin(); }
inline const_iterator cbegin() const noexcept { return elements.cbegin(); }
inline iterator end() noexcept { return elements.end(); }
inline const_iterator cend() const noexcept { return elements.cend(); }
eleType& AddElement(keyType key, eleType element);
eleType& operator[] (keyType key)
{
return elements[key];
}
private:
std::map<keyType, eleType> elements;
};
}
}
Global.cpp
//global.cpp
#include "global.h"
#include "MElement.h"
using namespace MapleStory::MGUI;
template <typename keyType, typename eleType>
MElementManager<keyType, eleType>::MElementManager()
{
}
template <typename keyType, typename eleType>
MElementManager<keyType, eleType>::~MElementManager()
{
for (auto element : elements)
{
if (element.second)
{
delete element.second;
element.second = 0;
}
}
}
template <typename keyType, typename eleType>
eleType& MElementManager<keyType, eleType>::AddElement(keyType key, eleType element)
{
elements.insert(std::pair<keyType, eleType>(key, element));
((MElement*)element)->SetName(key);
return this->elements[key];
}