[완료][c++]자료구조 stack을 공부하고 있는데 template적용이 안돼요.

interoasis의 이미지

//In file Stack.h
#include <iostream>
 
using namespace std;
 
template <class T> 
class Stack
{
         public:
                 Stack();                        
                 void Push(const T parameter);  
                 T Pop();                       
                 int Size() const;               
                 bool IsEmpty() const;            
         private:
                 T *stack;                     
                 T *top;                        
                 int size;                      
};

//In file Stack.cpp
#include "Stack.h"
 
template <class T>
Stack<T>::Stack()
{
        stack = new T[10];
        top = stack;
        size = 0;
}
 
template <class T>
void Stack<T>::Push(const T parameter)
{
        top[++size] = parameter;
}
 
template <class T>
T Stack<T>::Pop()
{
        return top[size--];
}
 
template <class T>
int Stack<T>::Size() const
{
        return size;
}
 
template <class T> 
bool Stack<T>::IsEmpty() const
{
        return size == 0;
}

//In file mainStack.cpp
#include "Stack.h"
 
int main(void)
{
        Stack<int> testStack;
 
        testStack.Push(3);
        testStack.Push(5);
        cout << testStack.Pop() << endl;
        cout << testStack.Pop() << endl;
}

이렇게 스택의 뼈대만 구현해서 컴파일을 하는데요. 에러가 납니다. 위의 T부분을 int로 바꿔주면 정상동작하구요. 책에서 template부분 봐가면서 쓴건데 왜 에러가 나는지 모르겠네요.

g++ -o Stack Stack.h Stack.cpp mainStack.cpp

위 명령어를 쳐서 컴파일하려면
/tmp/cciKn6zJ.o: In function `main':
mainStack.cpp:(.text+0x11): undefined reference to `Stack<int>::Stack()'
mainStack.cpp:(.text+0x35): undefined reference to `Stack<int>::Push(int)'
mainStack.cpp:(.text+0x49): undefined reference to `Stack<int>::Push(int)'
mainStack.cpp:(.text+0x55): undefined reference to `Stack<int>::Pop()'
mainStack.cpp:(.text+0x81): undefined reference to `Stack<int>::Pop()'
collect2: ld returned 1 exit status

이런 에러를 뿜으며 컴파일이 안됩니다.분명히 다 정의돼있는 함수인데 정의가 안돼있다고하니...;;
고수님들 답변좀 부탁드릴께요.
winner의 이미지

C++ 를 차근차근 해나가는 사람들이 한번쯤 하는 어려움이죠. 아직까지 template은 모두 header file 에 정의되어야 합니다.

ifree의 이미지

템플릿은 사용되는 구체적인 타입으로 바뀌어 컴파일이 됩니다.(instantiation)

그런데 main.cpp 에 헤더 파일만 include 되기 때문에 헤더부분은 T 가 정수 타입인 클라스로 컴파일이 되지만, 별도의 Stack.cpp 파일은 구체적인 타입이 지정되지 않아 컴파일조차 되지 않고, 따라서 링크 타임 에러가 나게 되죠.

interoasis의 이미지

이럴수가... 이것때문에 2주일을 보냈는데... ㅠ.,ㅜ 답변 정말 감사드립니다. 왜 이런 중요한 내용이 책에는 안나와있는걸까요? 크흑~

neocoin의 이미지

boost 소스 한번 열어 보세요. hpp 라는 확장자를 곧잘 씁니다.

댓글 달기

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