c++ 미로찾기

sophieeekim의 이미지

#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 ==========

라는 오류가 나네요 ㅠㅠㅠ 방법 없을까요?ㅜㅠㅠㅠㅠㅠ

익명 사용자의 이미지

std::basic_ostream에서 Stack type을 어떻게 출력해야 하는지 몰라서 에러가 난 겁니다.
필요없는 부분이라면 해당 라인을 삭제하거나
stream과 stack type을 받을 수 있는 << operator를 새로 구현하던가
stack을 string으로 변환하는 함수 toString() 같은 걸 만들어
cout << toString(stack); 처럼 쓰면 됩니다.

댓글 달기

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