클래스 템플릿의 멤버함수에서 기본인수를 어떻게 언급해야 하나요?

dltkddyd의 이미지

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);

dltkddyd의 이미지

이상하게도 클래스 내부에서 정의하고 기본인수의 값을 언급하니 컴파일 에러 없이 컴파일이 제대로 됩니다. 이런 멤버함수는 인라인 함수인데.
또 이상한 것은 외부에서 inline 키워드를 사용해서 해당 멤버함수를 정의하려 하면 또 에러가 발생합니다. 이 문제는 또 어떻게 해결해야 할까요?

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

익명 사용자의 이미지

template class는 일반적으로 선언과 구현을 따로 하지 않고 하나로 작성합니다.

dltkddyd의 이미지

그런데도 질문드린 문제가 발생한 겁니다.

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

kukyakya의 이미지

템플릿과는 상관없이 기본 인자값은 함수의 선언에 넣어주셔야합니다.

template <class targ> class DynamicArr {
	bool fill(targ src, unsigned firstidx, unsigned lastidx = 0);//기본값  ㅣastidx=0
};
 
template <class targ> bool DynamicArr<targ>::fill(targ src, unsigned firstidx, unsigned lastidx) {
	// 함수 정의
}
dltkddyd의 이미지

계속 같은 문제가 발생했습니다.

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

익명 사용자의 이미지

Quote:

클래스 내부에서 정의하고 기본인수의 값을 언급하니 컴파일 에러 없이 컴파일이 제대로 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.