2017-12-04 5 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; 
} 
링커에 의해

임 아직도 점점 오류 :

LNK2019 확인되지 않은 외부 기호 "공공 : 부울 __thiscall 저장 :: writeFile (class std :: basic_string, class std :: allocator> 클래스 AFile *) " (?? $ writeFile @ VAFile @@@ Storage @@ QAE_NV? $ basic_string @ DU? $ char_traits @ D @ std @@ V ? $ allocator @ D @ 2 @@ std @@ PAVAFile @@@ Z) 함수 _main

코드가 올바르게 표시되는 이유는 무엇입니까? 클래스의 메서드에 대한 일반적인 정의와 메서드에 전달되는 클래스 형식을 제한하려고합니다. 그리고 AFile은 ABase로부터 상속됩니다.

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

음 ... 첫째, 오류 메시지는 함수가 실제로 클래스 멤버 (클래스'Storage'의 멤버)임을 나타냅니다. 그러나 귀하의 선언이나 귀하의 정의는 그것을 집단 구성원으로 정의하지 않습니다. 어째서? 둘째, 어떻게 그것이 "사용"의 자격을 얻는가? 나는 당신의 "사용법"에'writeFile'에 대한 언급을 보지 못했습니다. – AnT

+0

질문이 업데이트되었습니다. 내가 한 모든 일 ... –

답변

0

이 연결 오류를 해결하려면 명시 적으로이 같은 Storage.cpp에서 [1] 템플릿 멤버 함수를 인스턴스화 할 수 있습니다 함수를 만들고 링커에서 찾을 수 있습니다.

헤더 파일의 정의 - Why can templates only be implemented in the header file?을 옮기는 것이 좋습니다.