c++ 미로찾기
글쓴이: sophieeekim / 작성시간: 금, 2015/03/20 - 9:03오후
#include <iostream> #include <algorithm> #include <iterator> #include <stack> using namespace std; #ifndef STACK_H #define STACK_H #define ROW 13 #define COL 17 template <class T> class Stack; struct Items { Items() {}; Items(int i, int j, int k) { x = i, y = j, dir = k; } int x, y, dir; }; template <class T> ostream& operator<<(ostream& os, Stack<T>& s){ os <<"top=" << s.top <<endl; for(int i=0; i<=s.top; i++) os << i << ":" << s.stack[i] <<endl; return os; } ostream& operator<<(ostream& os, Items& item){ return os<< item.x <<"," <<item.y << "," << item.dir; } template <class T> class Stack{ private: T* stack; //스택 원소를 위한 배열 int top;//top 원소의 위치 int capacity;//스택 배열의 크기 void ChangeSize1D(T*& a, const int oldSize, const int newSize); public: Stack(int stackCapacity =10); bool IsEmpty() const; T& Top()const;//top 원소 반환 void Push(const T& item); // top에 아이템 삽입 void Pop(); //스택의 top 원소 삭제 friend ostream& operator<< (ostream& , Stack<T>& ); }; template<class T> Stack<T>::Stack(int stackCapacity): capacity(stackCapacity){ if(capacity <1) throw "Stack capacity must be >0"; stack = new T[capacity]; top = -1; } template<class T> inline bool Stack<T>::IsEmpty() const {return top ==-1;} template<class T> inline T& Stack<T>::Top() const { if(IsEmpty()) throw "Stack is empty"; return stack[top]; } template <class T> void Stack<T>::Push(const T& x){ if(top == capacity -1){ ChangeSize1D(stack, capacity, 2*capacity); capacity*=2; } stack[++top] = x; } template <class T> void Stack<T>::Pop() { if(IsEmpty()) throw "Stack is empty. Cannot delete"; stack[top--].~T(); } template<class T> void Stack<T>::ChangeSize1D(T*& a, const int oldSize, const int newSize) { if(newSize<0) throw "New length must be >= 0"; T* temp = new T[newSize]; // 새로운 배열 int number = min(oldSize, newSize); // 복사할 원소 수 copy(a, a+number, stdext::checked_array_iterator<T*>(temp, number)); delete []a; // 이전 메모리 제거 a = temp; } struct offsets { int a, b; }; enum directions { N, NE, E, SE, S, SW, W, NW }; offsets moveto[8] = { {-1,0}, {-1,1}, {0,1}, {1, 1}, {1, 0}, {1, -1}, {0,-1}, {-1, -1}}; #endif #include <iostream> #include <algorithm> #include <stack> #include<iterator> using namespace std; #include "stack.h" int mark[ROW][COL] ={0}; int maze[ROW][COL] ={ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1}, { 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1}, { 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1 }, { 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1}, { 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1}, { 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1}, { 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1}, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; void Path(){ int m =ROW-2; int p =COL-2; mark[1][1] = 1; Stack<Items> stack(m*p); Items temp(1,1,E); stack.Push(temp); while(!stack.IsEmpty()){ temp=stack.Top(); stack.Pop(); int i= temp.x; int j= temp.y; int d= temp.dir; while(d<8){ int g= i+moveto[d].a; //move forward int h= j+moveto[d].b; if((g==m)&&(h==p)){ cout<<stack; cout<<i<<" "<<j<<endl; cout<<m<<" "<<p<<endl; return; } if((!maze[g][h])&&(!mark[g][h])){ mark[g][h] =1; temp.x=i; temp.y=j; temp.dir =d+1; stack.Push(temp); i=g; j=h; d=N; } else d++; } } cout <<"No path in maze" <<endl; } void main(){ Path(); }
미로찾기프로그램인데 자꾸
main.obj : error LNK2019: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Stack &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Stack@UItems@@@@@Z) 외부 기호(참조 위치: "void __cdecl Path(void)" (?Path@@YAXXZ) 함수)에서 확인하지 못했습니다.
1>C:\Users\Administrator\Documents\Visual Studio 2010\Projects\2013198064\Debug\2013198064.exe : fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다.
========== 빌드: 성공 0, 실패 1, 최신 0, 생략 0 ==========
라는 오류가 나네요 ㅠㅠㅠ 방법 없을까요?ㅜㅠㅠㅠㅠㅠ
Forums:
cout<<stack 에서 에러가 난 거네요.
std::basic_ostream에서 Stack type을 어떻게 출력해야 하는지 몰라서 에러가 난 겁니다.
필요없는 부분이라면 해당 라인을 삭제하거나
stream과 stack type을 받을 수 있는 << operator를 새로 구현하던가
stack을 string으로 변환하는 함수 toString() 같은 걸 만들어
cout << toString(stack); 처럼 쓰면 됩니다.
댓글 달기