가변클래스 템플릿에서 생성자를 어떤 식으로 만들어야 하나요?

dltkddyd의 이미지

가변클래스템플릿을 만들었습니다. 그런데 컴파일시 다음과 같은 오류가 뜹니다.

file.cc: In constructor 'AnyExtraction:AnyExtraction(ExtractedType, Args ...)[with ExtractedType=double, Args={int, float}]';
file.cc: instantiated from here
file.cc:error: type 'AnyExtraction' is not a direct base of 'AnyExtraction'

#include <iostream>
using namespace std;
#include <type_traits>
 
template<typename ExtractedType=int, typename... Args>
class AnyExtraction {
public:
	static const int value=0;
	ExtractedType exValue;
	AnyExtraction(ExtractedType _exValue, Args... args):AnyExtraction(args...) {//여기에서 에러가 난답니다.
		exValue=_exValue;
		cout<<"exValue is "<<exValue<<endl;
		cout<<"The number of remainers is "<<sizeof...(Args)<<endl;
	}
	AnyExtraction(ExtractedType _exValue) {
		exValue=_exValue;
		cout<<"exValue is "<<exValue<<endl;
	}
};
 
int main() {
	AnyExtraction<double,int, float> obj1(5.5, 10, 4.5);//여기 이 객체가 생성될 때
	return 0;
}

제가 예상했던 결과는 이렇습니다. obj1이라는 객체가 생성되는 과정에서 생성자가 호출될 때 ExtractedType은 타입을 하나씩 추출해서 exValue라는 멤버에 순서대로 값을 저장하도록 합니다. 그리고 결과를 cout으로 출력하기 전에 이니셜라이져로 생성자를 호출해서 다음 값을 순서대로 exValue에 저장합니다. 이런 식으로 하면

exValue is 4.5
exValue is 10
The number of remainers is 1
exValue is 5.5
The number of remainer is 2

그런데 에러부터 납니다. 무엇을 어떻게 고쳐야 의도하던대로 이 클래스가 작동될까요?

klyx의 이미지

템플릿 클래스는 인자가 바뀌면 다른 타입이 됩니다. 예를 들어 vector<int>와 vector<double>은 다른 타입니다.
delegate constructor를 사용하는 것이므로 당연히 같은 타입의 생성자만 호출이 가능합니다.
인자가 하나줄어든 템플릿 클래스는 다른 타입의 템플릿 클래스이므로 이렇게 짜는건 불가능합니다.
생성자가 아닌 함수를 이용하세요.

dltkddyd의 이미지

답변 감사합니다.

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

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.