2017-12-04 11 views
0
#include <iostream> 
#include <fstream> 
#include <type_traits> 
#include <Windows.h> 

#include "abase.h" 

using namespace std;  
class Storage { 
    string _path; 

public: 
    Storage(string path); 
    ~Storage() = default; 
    template <typename T > 
     bool writeFile(string fileName, 
      typename enable_if<is_base_of<ABase, T>::value, T >::type* data); 
} 

定義...C++ 11リンカエラーVS17

#include "storage.h" 

Storage::Storage(string path) 
{ 
    this->_path = path; 
} 
template <typename T > 
     bool Storage::writeFile(string fileName, 
      typename enable_if<is_base_of<ABase, T>::value, T >::type* data){ 
     return true; 
} 

Imはまだリンカによってエラーを取得:

LNK2019未解決の外部シンボル「パブリック:ブール__thiscall ストレージ:: writeFile(クラスstd :: basic_string、クラスstd :: allocator>、クラスAFile *) " (?? $ writeFile @ VAFile @@@ Storage @@ QAE_NV?$ basic_string @ DU?$ char_traits @ D @ std @@ V ?$ allocator @ D @ 2 @@ std @@ PAVAFile @@@ Z) 関数内_Main

コードが正しく表示されているのはなぜですか。クラス内のメソッドの一般的な定義とメソッドに渡すクラス型を制約しようとするIm AFaseはABaseから継承されています。

アバースは抽象クラスです。メインで

簡単な利用法:そう

template 
bool Storage::writeFile<AFile>(string fileName, 
    enable_if<is_base_of<ABase, AFile>::value, AFile>::type* data); 

コンパイラ:

Storage* s = new Storage("C:\\aPath..."); 
AFile* afile = new AFile(); 
s->writeFile<AFile>("a.txt", afile); 
+0

s = Storageのオブジェクト –

+0

Um ...まず、関数が実際にはクラスメンバ(クラス 'Storage'のメンバ)であることを示すエラーメッセージが表示されます。しかし、あなたの宣言も定義も、それをクラスのメンバーとして定義していません。どうして?第二に、それはどのように "使用"と見なされますか?あなたの "使用法"に 'writeFile'の言及はありません。 – AnT

+0

質問が更新されました。私が行ったことすべて... –

答えて

0

あなたのリンクエラーを解決するには、明示的にこのようなStorage.cppで[1]テンプレートのメンバ関数をインスタンス化することができます関数を作成し、リンカーが見つけることができます。

ヘッダーファイルの定義を移動する方が良いです - Why can templates only be implemented in the header file?