2012-04-01 86 views
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個文件,並且我下面給出了它們。

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; 
} 

回答

5

你必須d efine模板在可見標題中起作用。

移動的

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。

+0

謝謝!但是,我有沒有辦法在不同的源文件中定義我的功能! 把我所有的功能放在標題本身上,看起來很醜,不是嗎?我應該把這個代碼作爲一個庫,所以在這種情況下,我的項目中只有一個大的emulator.h文件! – Microkernel 2012-04-01 05:15:51

+0

@Microkernel:是的,這就是您需要執行模板功能的方式。流行的[Boost](http://www.boost.org/)庫幾乎完全*在頭文件中實現。 – 2012-04-01 05:17:01

+0

@Microkernel:這被稱爲僅包含頭文件的庫,在C++中是相當常見的範例。感覺良好的事實是,只有C++頭文件的庫可以跳過高樓,比速度快的子彈更快。編譯器可以進行各種編譯時優化,否則不可能。 – 2012-04-01 05:17:12

相關問題