我知道错误从何而来。我已经在C++代码下面标记了它。我不知道这个错误。希望有人帮帮我。如何修复这个问题?
错误为:体系结构x86_64的符号未定义:“operator<<(std::__1::basic_ostream
#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;
}
运算符<<
的友元声明与现有的运算符<<
定义不匹配。它也必须声明为模板,否则它将被视为一个单独的非模板函数,在重载解析过程中会被首选,但不会实现:
template<class xx_T>
friend ostream &operator<<(ostream &os, Stack<xx_T> &s);