C++의 템플릿으로 일반화된 Stack 자료구조를 구현하는데 컴파일 오류가 있습니다..

lhc9321의 이미지

//Stack.h

#pragma once

template
class Node
{
private:
E& elem;
Node* next;
friend class Stack;
};

template
class Stack
{
public:
Stack();
~Stack();
void push(const E& e);
void pop();
const E& top() const;
const bool empty() const;
const int size() const;
private:
Node* head;
Node* tail;
Node* current;
int capacity;
};

template
Stack::Stack() {
head = nullptr;
tail = nullptr;
current = nullptr;
capacity = 0;
}

template
Stack::~Stack()
{
if (head != nullptr) {
current = head->next;
delete head;
head = current;
}
}

template
void Stack::push(const E & e)
{
current = new Node;
current->elem = e;
current->next = head;
head = current;
capacity++;
}

template
void Stack::pop()
{
if (head != nullptr) {
current = head->next;
delete head;
head = current;
capacity--;
}
}

template
const E & Stack::top() const
{
return head->elem;
}

template
const bool Stack::empty() const
{
if (capacity != 0) return false;
else return true;
}

template
const int Stack::size() const
{
return capacity;
}

///////////////////////////////////////////////////////////////

//Stack.cpp
#include "Stack.h"
#include
#include
using namespace std;

int main() {
Stack a ;
a.pop();
a.push(7);
cout << "사이즈: " << a.size() << endl;
a.push(13);
cout << a.top() << endl;
a.pop();
a.push(9);
cout << a.top() << endl;
cout << a.top() << endl; a.pop();
Stack B;
B.push("bob");
B.push("Alice");
cout << B.top() << endl; B.pop();
B.push("Eve");
return 0;
}

//////////////////////////////////////////////

스택 헤더와 구현을 따로 했더니 템플릿은 그렇게 하면 안되고 헤더에 구현까지 넣어야한다고 해서 그렇게 했고,

메인함수는 cpp내에 했는데 계속 템플릿 관련 오류가 뜹니다.. 몇시간째 이것과 사투를 벌이고 있습니다..

visual studio 2015 쓰고 있구요.. 꼭좀 도와주세요

pro0331의 이미지

VS는 안써봐서 정확하진 않지만...

template
class Stack {
...

이런식으로 선언하는거랑,
클래스 선언 안에 함수 구현까지 다 넣고,
사용할 때 Stack a; 이런식으로 해야 하지 않나 싶네요.

pro0331의 이미지

아... 코드가 이상하다 싶더라니 꺽쇠가 사라지는거였군요 ㅋㅋㅋ

HDNua의 이미지

1. 실행은 안 해보고, 컴파일 오류가 뜨는 것만 해결한 것입니다.
비교해보세요. (주석으로 이전 코드와 달라진 부분을 강조했습니다.

Stack.h

#pragma once
 
template <class E>
class Node
{
private:
    E elem; // E& elem
    Node* next;
 
    template <class E>
    friend class Stack;
 
public:
    // Node 생성자 정의 추가
    Node(const E& elem) : elem(elem), next(nullptr) {
 
    }
};
 
 
 
template <class E>
class Stack
{
public:
    Stack();
    ~Stack();
    void push(const E& e);
    void pop();
    const E& top() const;
    const bool empty() const;
    const int size() const;
private:
    Node<E>* head; // Node* head;
    Node<E>* tail; // Node* tail;
    Node<E>* current; // Node* current;
    int capacity;
};
 
template <class E>
Stack<E>::Stack() { // Stack::Stack() {
    head = nullptr;
    tail = nullptr;
    current = nullptr;
    capacity = 0;
}
 
template <class E>
Stack<E>::~Stack() // Stack::~Stack()
{
    if (head != nullptr) {
        current = head->next;
        delete head;
        head = current;
    }
}
 
template <class E>
void Stack<E>::push(const E & e) // void Stack::push(const E & e)
{
    current = new Node<E>(e); // current = new Node;
    current->elem = e;
    current->next = head;
    head = current;
    capacity++;
}
 
template <class E>
void Stack<E>::pop() // void Stack::pop()
{
    if (head != nullptr) {
        current = head->next;
        delete head;
        head = current;
        capacity--;
    }
}
 
template <class E>
const E & Stack<E>::top() const // const E & Stack::top() const
{
    return head->elem;
}
 
template <class E>
const bool Stack<E>::empty() const // const bool Stack::empty() const
{
    if (capacity != 0) return false;
    else return true;
}
 
template <class E>
const int Stack<E>::size() const // const int Stack::size() const
{
    return capacity;
}

main.cpp

#include <iostream>
#include "Stack2.h"
using namespace std;
 
int main2() {
    Stack<int> a; // Stack a;
    a.pop();
    a.push(7);
    cout << "사이즈: " << a.size() << endl;
    a.push(13);
    cout << a.top() << endl;
    a.pop();
    a.push(9);
    cout << a.top() << endl;
    cout << a.top() << endl; a.pop();
    Stack<const char *> B; // Stack B;
    B.push("bob");
    B.push("Alice");
    cout << B.top() << endl; B.pop();
    B.push("Eve");
    return 0;
}

저는 이렇게 생각했습니다.

shint의 이미지

...

댓글 첨부 파일: 
첨부파일 크기
Package icon test C++ 템플릿과 스택 구현.zip387.58 KB

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

댓글 달기

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