[질문]볼렌드c++에서 template을 사용하는데 에러가 ㅡㅜ
글쓴이: hshthsh / 작성시간: 금, 2004/08/13 - 4:01오후
안녕하세요.
볼렌드 c++5.5를 이용해 template함수를 정의했는데 에러가 나는데 도통 이유를 모르겠습니다. 아래와 같이 정의했더니...
template <> double _max<box>(const box & b1,const box & b2)
{
return (b1.volume > b2.volume)?b1.volume:b2.volume;
}
이런 에러가 나와서..
^Type mismatch in redeclaration of '_max<box>(const box &,const box &)'
^Earlier declaration of '_max<box>(const box &,const box &)'
전달인자에const만 빼고 다시 이렇게 바꿔보니..
template <> double _max<box>(box & b1,box & b2)
{
return (b1.volume > b2.volume)?b1.volume:b2.volume;
}
이제는 이런에러가 나더니..
^Explicit specialization or instantiation of non-existing template '_max'
마지막으로 이렇게 바꾸니 에러가 나지 않던데 혹시 이유를 하시는분 ㅡㅜ
double _max<box>(box & b1,box & b2)
{
return (b1.volume > b2.volume)?b1.volume:b2.volume;
}
Forums:


[code:1]template <> double _max&
template <> double _max<box>(const box & b1,const box & b2) { return (b1.volume > b2.volume)?b1.volume:b2.volume; }템플릿에서 위와 같은 코드가 원래 컴파일 되는건가요?
저라면
template <class box> double _max(const box & b1,const box & b2) { return (b1.volume > b2.volume)?b1.volume:b2.volume; }이렇게 코딩했을 것 같은데..
서로 기능이 다른가요?
템플릿 특수화를 하려고 하셨던 겁니까? 그렇다면 다음과 같이 일반적인 것
템플릿 특수화를 하려고 하셨던 겁니까? 그렇다면 다음과 같이 일반적인 것을 먼저 정의해야죠.
#include<iostream> struct box { double v; }; template <class T> double max(const T& b1, const T& b2) { return (b1>b2)?b1:b2; } template <> double max<box>(const box& b1, const box& b2) { return (b1.v>b2.v)?b1.v:b2.v; } int main() { double a=1; double b=2; box ba = {3}; box bb = {4}; std::cout << max(a,b) <<std::endl; std::cout << max(ba,bb) << std::endl; }댓글 달기