#include<iostream>
#include<assert.h>
using namespace std;
template<class ItemType>
struct NodeType;
template<class ItemType>
class StackType
{
public:
StackType();
~StackType();
void push(ItemType jungsu, ItemType jinsu);
void pop();
ItemType Top();
bool IsFull() const;
bool IsEmpty() const;
private:
NodeType* topPtr;
};
template<class ItemType>
struct NodeType{
ItemType info;
NodeType* next;
};
template<class ItemType>
StackType<ItemType>::StackType()
{
topPtr = NULL;
}template<class ItemType>
StackType<ItemType>::~StackType()
{
NodeType* tempPtr;
while (topPtr != NULL)
{
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
template<class ItemType>
void StackType<ItemType>::push(ItemType jungsu, ItemType jinsu)
{
if (IsFull())
cout << "Error : the Stack is Full" << endl;
else
{
while (jungsu > 0)
{
NodeType* location;
location = new NodeType;
location->info = jungsu%jinsu;
jungsu = jungsu / jinsu;
location->next = topPtr;
topPtr = location;
}
}
}
template<class ItemType>
void StackType<ItemType>::pop()
{
if (IsEmpty())
{
cout << "Error : The Stack Is Empty" << endl;
}
else
{
NodeType* tempPtr;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
template<class ItemType>
ItemType StackType<ItemType>::Top()
{
if (IsEmpty())
cout << "Error : The Stack Is Empty 표시할 것 이없습니다." << endl;
else
return topPtr->info;
}
template<class ItemType>
bool StackType<ItemType>::IsEmpty() const{
if (topPtr == NULL)
return true;
else
return false;
}template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
NodeType* location;
try
{
location = new NodeType;
delete location;
return false;
}
catch (std::bad_alloc exception)
{
return true;
}
}
template<class ItemType>
int main()
{
StackType convert;
ItemType jungsu, jinsu;
cout << "정수와 진수를 차례대로 입력하세요(16진수 이상)";
cin >> jungsu >> jinsu;
assert(jinsu <= 16);
assert(jungsu > 0 && jinsu > 0);
convert.push(jungsu, jinsu);
while (!convert.IsEmpty())
{
if (convert.Top() == 10){
cout << 'A';
convert.pop();
}
else if (convert.Top() == 11){
cout << 'B';
convert.pop();
}
else if (convert.Top() == 12){
cout << 'C';
convert.pop();
}
else if (convert.Top() == 13){
cout << 'D';
convert.pop();
}
else if (convert.Top() == 14){
cout << 'E';
convert.pop();
}
else if (convert.Top() == 15){
cout << 'F';
convert.pop();
}
else
{
cout << convert.Top();
convert.pop();
}
}
cout << endl;
return 0;
}
메인함수에 템플릿이라니..
근데 에러메세지 안읽음?
http://codepad.org/p2FBf2D5
메인에 template선언안해주면 빨간줄존내뜸 나도당황;
메인을 템플릿으로 선언하고 외부 참조 에러라니... ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
이 멍충아.. 메인을 템플릿으로 선언하면 메인이 없는거잖아
근데 메인에 template선언안해주면 StackType하고 ItemType이 뭘말하는지 몰라서 빨간줄뜸
ItemType<int> 이렇게 선언해야지;;
와 ㅅㅂ 생각지도 못했던 발상이다..... 메인함수를 템플릿으로 정의하다니...
난 여태껏 단 한번도 메인함수를 템플릿으로 작성해본적이 없고 시도해본적도, 상상해본적도 없는데 신선한 충격이군
나도 main에 선언하면안되는거는 방금이해했는데.. 왜 StackType하고 ItemType을 못읽을까 main에서만; 왜그럴까?
참고로 main에서 StackType<int> convert;인식 못함;
아니 StackType<int> convert;인식못해
??<int>
StackType ( int)
스택타입 클래스에서도 잘못했네 NodeType* topPtr; 이게아니라 NodeType<ItemType>* topPtr; 이렇게 해야지
??????????????????????????? 뭐가다른거??ㅋㅋㅋㅋㅋㅋㅋ
뭐가다르다니 NodeType* 이게 무슨클래스인지알아? 템플릿 인수가없는데;
메인함수안에서 ItemType 쓰지말구 정수랑 진수
아 디시 댓글에 중괄호안먹힘 NodeType (ItemType) * topPtr이렇게 하라고?
<>이거로 ㅇㅇ
중괄호가 안보이나보넹 내껀 모바일이라 보이나