模板构造函数中的C++链接器错误:"unresolved external symbol"
C++ linker error in template constructor: "unresolved external symbol"
我正试图用C++编写一个模板类,但遇到了这个奇怪的链接器错误,无法找出原因,请告诉我这是怎么回事!
这是我在Visula C++2010中得到的错误消息。
1>------ Rebuild All started: Project: FlashEmulatorTemplates, Configuration: Debug Win32 ------
1> main.cpp
1> emulator.cpp
1> Generating Code...
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall flash_emulator<char>::flash_emulator<char>(char const *,struct FLASH_PROPERTIES *)" (??0?$flash_emulator@D@@QAE@PBDPAUFLASH_PROPERTIES@@@Z) referenced in function _main
1>C:ProjectsFlashEmulator_templatesVSFlashEmulatorTemplatesDebugFlashEmulatorTemplates.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
g++中的错误消息
main.cpp: In function âint main()â:
main.cpp:8: warning: deprecated conversion from string constant to âchar*â
/tmp/ccOJ8koe.o: In function `main':
main.cpp:(.text+0x21): undefined reference to `flash_emulator<char>::flash_emulator(char*, FLASH_PROPERTIES*)'
collect2: ld returned 1 exit status
有两个.cpp文件和一个头文件,我在下面给出了它们。
模拟器.h
#ifndef __EMULATOR_H__
#define __EMULATOR_H__
typedef struct {
int property;
}FLASH_PROPERTIES ;
/* Flash emulation class */
template<class T>
class flash_emulator
{
private:
/* Private data */
int key;
public:
/* Constructor - Opens an existing flash by name flashName or creates one with
given FLASH_PROPERTIES if it doesn't exist */
flash_emulator( const char *flashName,
FLASH_PROPERTIES *properties );
/* Constructor - Opens an existing flash by name flashName or creates one with
given properties given in configFIleName */
flash_emulator<T>( char *flashName,
char *configFileName );
/* Destructor for the emulator */
~flash_emulator(){
}
};
#endif /* End of __EMULATOR_H__ */
模拟器.cpp
#include <Windows.h>
#include "emulator.h"
using namespace std;
template<class T>flash_emulator<T>::flash_emulator( const char *flashName,
FLASH_PROPERTIES *properties )
{
return;
}
template<class T>flash_emulator<T>::flash_emulator(char *flashName,
char *configFileName)
{
return;
}
main.cpp
#include <Windows.h>
#include "emulator.h"
int main()
{
FLASH_PROPERTIES properties = {0};
flash_emulator<char> myEmulator("C:newEMu.flash", &properties);
return 0;
}
您必须在可见标头中定义模板函数。
移动的定义
template<class T> flash_emulator<T>::flash_emulator( const char *flashName,
FLASH_PROPERTIES *properties )
和
template<class T> flash_emulator<T>::flash_emulator(char *flashName,
char *configFileName)
从emulator.cpp到emulator.h。
相关文章:
- CppSQLite 抛出"LNK2019: unresolved external symbol"错误
- Visual Studio中的wxwidgets应用程序给出错误"LNK2019 unresolved external symbol"
- 我的游戏引擎的 spdlog 日志记录出现奇怪的"unresolved external symbol"错误
- Visual Studio 2017 中的 MySQL 连接器/C++ 错误"Unresolved external symbol _get_driver_instance"
- CMake 和 Visual Studio 出现"unresolved external symbol"错误
- "unresolved overloaded function type"错误并将运算符作为函数参数传递
- 错误 : "error LNK2019: unresolved external symbol"
- 为什么我的代码生成"Unresolved external symbol"错误?
- 收到错误,即"unresolved external symbol"
- Qt 5.4 静态构建在 Visual Studio 2013 中产生"unresolved external symbol"链接错误
- 我收到错误"LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup"
- 'unresolved external symbol _SDL_main referenced in function' Visual Studio 2012 中的 SDL2 错误
- 模板构造函数中的C++链接器错误:"unresolved external symbol"
- LNK 2019 "Unresolved External Symbol"错误 (C++ OpenGL)
- MVS2015 SDL2 不断给出"unresolved externals"错误
- 生成错误"LNK2019: unresolved external symbol _WinMain@16"
- 我得到这个错误是Visual Studio C++ :: "error LNK2019: unresolved external symbol"
- 如何解决 DirectX 11 程序中"unresolved external symbol"错误?
- 未解析的外部符号在使用/force: Unresolved时显示为错误
- C++ cURL 链接错误"unresolved external symbol _curl_easy_"