Undefined symbols for architecture x86_64(我发现了错误,但无法修复。)

Undefined symbols for architecture x86_64 (I have found error, but I can not fix it.)

我知道错误从何而来。我已经在c++代码下面做了标记。 我不知道这个错误。希望有人帮助我。如何解决这个问题?

错误是: 未定义的体系结构符号 x86_64: “运算符<<(std::__1::basic_ostream&, Stack&)”,引用自: main.o 中的路径(整数,整数) ld:未找到体系结构的符号 x86_64 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

#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;
}

operator << 的朋友声明与现有的 operator << 定义不匹配。它也必须被声明为模板,否则它被视为一个单独的非模板函数,在重载决策期间将被首选但不会实现:

template<class xx_T>
friend ostream &operator<<(ostream &os, Stack<xx_T> &s);

这是一个well-known problem有模板的朋友。问题在于编译器将友元声明视为非模板,但它 模板。如果您坚持 operator<< 的 out-of-class 定义,您可以执行以下操作:

template<class T>
class Stack;

template<class T>
std::ostream& operator<<(std::ostream&, const Stack<T>&);

template<class T>
class Stack {
public:
    friend std::ostream &operator<< <> (std::ostream&, const Stack<T>&);
};

template<class T>
std::ostream& operator<<(std::ostream&, const Stack<T>&) {
    //...
}

换句话说,你应该首先告诉编译器 friend operator<< 是一个模板。

Demo