2013-06-15 3 views
0

벡터 요소를 인쇄하는 템플릿 클래스가 있습니다. 나는 포인터와 참조 버전 둘 다 가지고있다.C++ 템플릿의 부분 특수화

// HEADER 
class Util { 
... 
template <class T> 
static void print(const std::vector<T>* vectorArray); 

template <class T> 
static void print(const std::vector<T>& vectorArray); 
... 
static void printByteStream(const std::vector<unsigned char>& input); 
... 
}; 

// BODY 
template <class T> 
void Util::print(const std::vector<T>* vectorArray) 
{ 
    for (auto i = vectorArray->begin(); i != vectorArray->end(); ++i) 
    { 
     std::cout << *i << ":"; 
    } 
    std::cout << std::endl; 
} 

template <class T> 
void Util::print(const std::vector<T>& vectorArray) 
{ 
    return Util::print(&vectorArray); 
} 

template void Util::print(const std::vector<int>* vectorArray); 
template void Util::print(const std::vector<std::string>* vectorArray); 
template void Util::print(const std::vector<int>& vectorArray); 
template void Util::print(const std::vector<std::string>& vectorArray); 

는 또한 바이트 스트림의 인쇄 코드가 있습니다.

void Util::printByteStream(const std::vector<unsigned char>& input) 
{ 
    for (auto val : input) printf("\\x%.2x", val); 
    printf("\n"); 
} 

나는 T == unsigned char로 print를 호출 할 때 부분적으로 전문화 된 printByteStream을 호출한다고 C++ 컴파일러에게 가르쳐 주려고합니다.

본문에이 코드를 추가했습니다.

void Util::print(const std::vector<unsigned char>& vectorArray) 
{ 
    return Util::printByteStream(vectorArray); 
} 

컴파일 할 때 C++ 컴파일러는 일치하는 코드를 찾을 수 없다고 불평합니다. 무엇이 잘못 되었을까요?

error: prototype for 'void Util::print(const std::vector<unsigned char>&)' does not match any in class 'Util' 
void Util::print(const std::vector<unsigned char>& vectorArray) 
+0

'Util'의 정의를 보여줄 수 있습니까? –

+0

일반 구현은 "본문"이 의미하는 것이면 별도의 파일로 갈 수 없습니다. – chris

+1

필요한 모든 특수화가 명시 적으로 인스턴스화되는 한, –

답변

2

난 당신이 내가 확인해야 빈 템플릿

template <> 
void Util::print(const std::vector<unsigned char>& vectorArray) 
{ 
    return Util::printByteStream(vectorArray); 
} 

을 추가 할 필요가 있다고 생각합니다. 나는이 컴퓨터에 VS가 없다.

+0

예, 문제 일 수 있습니다. 그것은 g ++ 컴파일러와 잘 동작합니다. – prosseek

관련 문제