클래스 템플릿의 멤버함수에서 기본인수를 어떻게 언급해야 하나요?
글쓴이: dltkddyd / 작성시간: 목, 2013/07/04 - 2:49오후
template <class targ> class DynamicArr { bool fill(targ src, unsigned firstidx, unsigned lastidx);//기본값 ㅣastidx=0 }; template <class targ> bool DynamicArr<targ>::fill(targ src, unsigned firstidx, unsigned lastidx=0) { if(lastidx==0) {lastidx=firstidx;} if(this->elements==NULL) { return false; } if(this->length-1<firstidx) { return false; } unsigned endidx=(this->length-1<lastidx)?this->length-1:lastidx; for(unsigned i=firstidx;i<=endidx;i++) { this->elements[i]=src; } return true; }
fill 멤버함수의 마지막 인수 lastidx의 기본값을 0으로 언급했습니다. 그런데 컴파일하면 다음과 같은 에러가 뜹니다.
main.cc: In function 'int main()':
mani.cc: 113.35: error: no matching function for call to 'DynamicArr::fill(char, unsigned int)'
main.cc: 113.35: note: candidate is:
In file included from main.cc: 3: 0:
DynamicArr.h: 309.28: note: bool DynamicArr::fill(targ, unsigned int, unsigned int) [with targ=char]
DynamicArr.h: 309: 28: note: candidate expects 3 arguments, 2 provided
대응되는 fill 멤버함수가 없다네요. main.cc 파일에서는 다음과 같이 fill 함수를 호출했습니다.
DynamicArr<char> d_arr3(strlen("Jump up up")+1); d_arr3="Jump up up"; d_arr3.fill('\0',d_arr3.count()-1);
Forums:
클래스 내부에서 정의하니 기본인수가 허용되네요..
이상하게도 클래스 내부에서 정의하고 기본인수의 값을 언급하니 컴파일 에러 없이 컴파일이 제대로 됩니다. 이런 멤버함수는 인라인 함수인데.
또 이상한 것은 외부에서 inline 키워드를 사용해서 해당 멤버함수를 정의하려 하면 또 에러가 발생합니다. 이 문제는 또 어떻게 해결해야 할까요?
본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.
template class는 일반적으로 선언과 구현을
template class는 일반적으로 선언과 구현을 따로 하지 않고 하나로 작성합니다.
네, 헤더 파일에 선언과 구현을 했습니다.
그런데도 질문드린 문제가 발생한 겁니다.
본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.
템플릿과는 상관없이 기본 인자값은 함수의 선언에
템플릿과는 상관없이 기본 인자값은 함수의 선언에 넣어주셔야합니다.
그렇게도 해봤지만...
계속 같은 문제가 발생했습니다.
본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.
Quote: 클래스 내부에서 정의하고 기본인수의 값을
댓글 달기