C++ C++;模板构造函数中的链接器错误:";未解析的外部符号“;
我试图在C++中编写一个模板类,并得到这个奇怪的链接错误,无法找出原因,请让我知道这有什么不对!p>C++ C++;模板构造函数中的链接器错误:";未解析的外部符号“;,c++,templates,linker-errors,C++,Templates,Linker Errors,我试图在C++中编写一个模板类,并得到这个奇怪的链接错误,无法找出原因,请让我知道这有什么不对!p> 这是我在VisualC++ 2010中得到的错误信息。 1>------ Rebuild All started: Project: FlashEmulatorTemplates, Configuration: Debug Win32 ------ 1> main.cpp 1> emulator.cpp 1> Generating Code... 1>main
这是我在VisualC++ 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:\Projects\FlashEmulator_templates\VS\FlashEmulatorTemplates\Debug\FlashEmulatorTemplates.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
1>----已启动全部重建:项目:FlashMulatorTemplates,配置:调试Win32------
1> main.cpp
1> emulator.cpp
1> 正在生成代码。。。
1> main.obj:错误LNK2019:未解析的外部符号“public:\u thiscall flash\u emulator::flash\u emulator(char const*,struct flash\u PROPERTIES*)”(??0?$flash_emulator@D@@QAE@PBDPAUFLASH_PROPERTIES@@@Z) 在函数_main中引用
1> C:\Projects\flashmulator\u templates\VS\flashmulatortemplates\Debug\flashmulatortemplates.exe:致命错误LNK1120:1未解析的外部
=========全部重建:0成功,1失败,0跳过==========
g中的错误消息++
main.cpp:intmain()函数中
main.cpp:8:警告:从字符串常量到–char*的转换已被弃用
/tmp/ccOJ8koe.o:在函数“main”中:
main.cpp:(.text+0x21):对“flash\u emulator::flash\u emulator(char*,flash\u属性*)”的未定义引用
collect2:ld返回了1个退出状态
有2个.cpp文件和1个头文件,我在下面给出了它们
emulator.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__ */
\ifndef\uuuh__
#定义仿真器__
类型定义结构{
int属性;
}闪光特性;
/*Flash仿真课程*/
模板
类flash_仿真器
{
私人:
/*私有数据*/
int键;
公众:
/*构造函数-按名称flashName打开现有闪存或使用
给定FLASH_属性(如果不存在)*/
flash_模拟器(const char*flashName,
闪光特性*特性);
/*构造函数-按名称flashName打开现有闪存或使用
configFIleName中给定的给定属性*/
flash_模拟器(char*flashName,
char*configFileName);
/*仿真器的析构函数*/
~flash_模拟器(){
}
};
#endif/*仿真程序的结尾*/
emulator.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;
}
#include <Windows.h>
#include "emulator.h"
int main()
{
FLASH_PROPERTIES properties = {0};
flash_emulator<char> myEmulator("C:\newEMu.flash", &properties);
return 0;
}
#包括
#包括“emulator.h”
使用名称空间std;
templateflash_emulator::flash_emulator(常量字符*flashName,
FLASH_属性*属性)
{
返回;
}
templateflash_emulator::flash_emulator(char*flashName,
char*configFileName)
{
返回;
}
main.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;
}
#include <Windows.h>
#include "emulator.h"
int main()
{
FLASH_PROPERTIES properties = {0};
flash_emulator<char> myEmulator("C:\newEMu.flash", &properties);
return 0;
}
#包括
#包括“emulator.h”
int main()
{
FLASH_属性={0};
flash\u模拟器myEmulator(“C:\newEMu.flash”、&properties);
返回0;
}
您必须在可见标题中定义模板函数
移动定义
template<class T> flash_emulator<T>::flash_emulator( const char *flashName,
FLASH_PROPERTIES *properties )
template flash\u emulator::flash\u emulator(const char*flashName,
FLASH_属性*属性)
及
template flash\u emulator::flash\u emulator(char*flashName,
char*configFileName)
从emulator.cpp到emulator.h.您必须在可见标题中定义模板函数 移动定义
template<class T> flash_emulator<T>::flash_emulator( const char *flashName,
FLASH_PROPERTIES *properties )
template flash\u emulator::flash\u emulator(const char*flashName,
FLASH_属性*属性)
及
template flash\u emulator::flash\u emulator(char*flashName,
char*configFileName)
从emulator.cpp进入emulator.h.谢谢!!!但是,我没有办法在不同的源文件中定义我的函数!!!把我所有的函数放在头文件本身会让它看起来很难看,不是吗?我应该把这段代码作为一个库来提供,所以在这种情况下,我的项目中只有一个大的emulator.h文件@微核:是的,这就是你需要做模板函数的方式。流行的库几乎完全在头文件中实现。@ MyNek:这就是一个头文件库,是C++中一个非常常见的范例。对C++头库能跳过高楼,比快子弹更快的事实感到高兴。编译器可以进行各种编译时优化,这在其他情况下是不可能的。。。因此,如果我的库被用在10个其他源文件中,那么当头文件被C++编译器替换为内容时,我将有10个相同函数的定义,对吗?谢谢:)@Microkernel别担心,相同的副本会被链接出来,你不会有多余的代码。谢谢!!!但是,我没有办法在不同的源文件中定义我的函数!!!把我所有的函数放在头文件本身会让它看起来很难看,不是吗?我应该把这段代码作为一个库来提供,所以在这种情况下,我的项目中只有一个大的emulator.h文件@微核:是的,这就是你需要做模板函数的方式。流行的库几乎完全在头文件中实现。@ MyNek:这就是一个头文件库,是C++中一个非常常见的范例。对C++头库能跳过高楼,比快子弹更快的事实感到高兴。编译器可以进行各种编译时优化,这在其他情况下是不可能的。。。因此,如果我的库被用在10个其他源文件中,那么当头文件被C++编译器替换为内容时,我将有10个相同函数的定义,对吗?谢谢:)@Microkernel别担心,相同的拷贝会被链接出来,你不会有多余的代码。