I know where error comes from. I have already marked it below the c++ code. I have no idea about this error. Hope someone help me. How to fix this problem?
The error is: Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream<char, std::__1::char_traits >&, Stack&)", referenced from: Path(int, int) in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
#include <iostream>
using namespace::std;
template<class T>
class Stack{
public:
Stack(int stackcapacity);
bool Isempty()const;
T& Top()const;
void Push(const T& item);
void Pop();
friend ostream &operator<<(ostream &os, Stack<T> &s);
private:
T* stack;
int top;
int capacity;
};
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)
{
T *temp=new T[2*capacity];
copy(stack,stack+capacity,temp);
delete[]stack;
stack=temp;
capacity*=2;
}
stack[++top]=x;
}
template<class T>
void Stack<T>::Pop(){
if(Isempty())
throw "stack is empty";
stack[top--].~T();
}
struct offsets{
int a,b;
};
enum directions{N,NE,E,SE,S,SW,W,NW};
struct Items{
int x,y,dir;
Items(){};
Items(int a,int b,int d){
x=a;
y=b;
dir=d;
}
};
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;
}
int maze[13][17] = {
{ 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, 0, 1, 1, 1, 0, 0, 1, 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(const int m,const int p){
const offsets move[8]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int mark[13][17]={0};
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+move[d].a;
int h=j+move[d].b;
if((g==m)&&(h==p)){
cout << stack; //error comes from this row
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";
}
int main(int argc, const char * argv[]) {
// insert code here...
Path(11,15);
return 0;
}