2017-11-13 3 views
0

안녕하세요 저는 바이너리 검색 트리에서 함수를 제대로 오버로드하는 방법을 궁금합니다. Heres는 코드는 내가 가진 : 함수를 제대로 오버로드하는 방법

int LessThan(E itm1, E itm2) { 
    if (itm1 < itm2) {return -1;} 
    else if (itm1 > itm2) {return 1;} 
    else if (itm1 == itm2) {return 0;} 
} 

int LessThan(string s1, string s2) { // Incase it is a string, we need to define <= and >= differently 
    return this->compare(s1, s2); 
} 

불행히도 나는 다음과 같은 오류를 받고 있어요 :

binarySearchTree.h:33:9: error: ‘int binarySearchTree<E>::LessThan(std::__cxx11::string, std::__cxx11::string) [with E = std::__cxx11::basic_string<char>; std::__cxx11::string = std::__cxx11::basic_string<char>]’ cannot be overloaded 
int LessThan(string s1, string s2) { // Incase it is a string, we need to define <= and >= differently 
    ^
binarySearchTree.h:27:9: error: with ‘int binarySearchTree<E>::LessThan(E, E) [with E = std::__cxx11::basic_string<char>]’ 
int LessThan(E itm1, E itm2) { 
    ^
+1

답변에 실제로 적절한 [mcve]가 포함되어야합니다. 클래스 템플릿의 멤버 함수는 ** 중요한 세부 사항 **입니다! – StoryTeller

답변

2

오버로딩이 문제에 대한 답이 아니다. 템플릿이 std::string으로 인스턴스화 될 때 특정 동작을 시도하고 있습니다. 적절한 기술은 명시 적 전문화입니다. 다음과 같이 템플릿 정의에 다음을 추가하십시오.

template<> 
int binarySearchTree<std::string>::LessThan(std::string s1, std::string s2) { 
    return this->compare(s1, s2); 
} 
+0

아 감사합니다. 나는 이것을 시험해 보겠다. – Rustykatz

관련 문제