0
C++でテンプレートクラスを作成しようとしていて、この奇妙なリンカエラーが発生しましたが、原因を特定できません。ここでテンプレートコンストラクタの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 *)" ([email protected]@@[email protected][email protected]@@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 ==========
グラムでのエラーメッセージ++
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
を取得していますエラーメッセージです私はそこに2つの.cppファイルと1つのヘッダファイルがあり、下にそれらを与えました。 dまであなたが持っている
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__ */
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;
}
main.cppに
#include <Windows.h>
#include "emulator.h"
int main()
{
FLASH_PROPERTIES properties = {0};
flash_emulator<char> myEmulator("C:\newEMu.flash", &properties);
return 0;
}
ありがとうございます!しかし、異なるソースファイルで自分の関数を定義する方法はありません! 私のすべての機能をヘッダそのものに入れることは、見た目が醜いのですか?私はこのコードをライブラリとして与えることになっています。その場合、私はプロジェクトで大きなemulator.hファイルを1つだけ持ちます! – Microkernel
@Microkernel:そうです、テンプレート関数を実行する必要があります。一般的な[Boost](http://www.boost.org/)ライブラリは、ヘッダファイルにほとんど*完全に実装されています。 –
@Microkernel:これはヘッダーのみのライブラリとして知られており、C++の非常に一般的なパラダイムです。 C++ヘッダーのみのライブラリは高層ビルを飛躍させることができ、スピードの速いビルドよりも高速です。コンパイラは、そうでなければ不可能なあらゆる種類のコンパイル時の最適化を行うことができます。 –