体系结构 x86_64 的未定义符号我发现了错误,但我无法修复它

问题描述

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

错误是: 体系结构 x86_64 的未定义符号: “operator&,Stack&)”,引用自: main.o 中的 Path(int,int) 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,};

void Path(const int m,const int p){
    const offsets move[8]={{-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,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<< 的类外定义,你可以这样做:

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>&) {
    //...
}

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

Demo