c++ template 클래스를 다루는데 문제가 생깁니다

count5084의 이미지


이런식으로 코딩을 해서 컴파일을 하는데,
g++ *.o -o exefile
링크단계에서
undefined reference to 'testClass::testMethod()'
등의 testMethod 가 정의되지 않았다는 에러가 발생합니다.
혹시 어떤 문제인지 도움을 주실 수 있나요~
처음 질문을 올려봐서 소스가 잘 안써져서 사진으로 올렸습니다 ㅜ
읽어주셔서 감사합니다.

File attachments: 
첨부파일 크기
Image icon 캡처.PNG23.91 KB
익명 사용자의 이미지

main.cpp에서 testClass에 대한 Implicit instantiation을 해야 하는 상황인데, main.cpp에서는 testClass<T>::testMethod()에 대한 정의(Definition)를 볼 수가 없어서 생기는 문제이지요.

대체로 이런 문제를 해결하는 방법은 두 가지입니다.

1. testMethod에 대한 정의를 header.h로 끌어 올리세요. 실제로 대부분의 템플릿 라이브러리들이 사용하는 방식입니다. 템플릿화 된 코드는 당연히 Implicit instantiation이 되어야 하므로 모든 코드를 헤더에 올려서 모든 사용자가 볼 수 있게 하는 것이죠.

2. Implicit instantiation을 포기하고, 예상되는 모든 T에 대해서 testClass<T>를 Explicit instantiation 해 주면 됩니다. 템플릿 라이브러리를 구현하는 게 아니라면 이렇게 하는 게 더 말이 되는 경우도 있습니다. 귀하의 경우에서는 class.cpp에서 해 주는 게 가장 적합하겠죠.

자세한 내용은 아래 링크를 참조하시길. 요즘 바빠서 자세한 답변을 달기가 어렵군요. 별로 보람도 없는 것 같고요:
https://en.cppreference.com/w/cpp/language/class_template

count5084의 이미지

1번에 경우 덕분에 이해가 됐습니다. 2번에 경우는 왜 그렇게 되는지 좀 더 이해를 해야할 것 같습니다.
링크 주신 것 참조해서 좀 더 알아보겠습니다. 제가 템플릿에 대한 공부가 너무 부족했던 것 같네요..
답변해주시는것에 대한 보람이 없다니 너무 아쉽습니다 ㅜ
덕분에 문제가 해결되고 좋은 공부가 되었습니다 감사합니다~

댓글 달기

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