C++ 架构x86_64的未定义符号(我发现了错误,但无法修复。)
我知道错误来自哪里。我已经在C++代码下面标记了它。 我不知道这个错误。希望有人能帮助我。如何解决这个问题 错误是: 架构x86_64的未定义符号: “运算符0”; 堆栈=新的T[容量]; top=-1; } 模板 内联布尔堆栈::Isempty()常量{ 返回顶部==-1; } 模板 内联T&Stack::Top()常量{ if(Isempty()) 抛出“堆栈为空”; 返回堆栈[顶部]; } 模板 无效堆栈::推送(常量T&x){ 如果(顶部==容量-1) { T*温度=新的T[2*容量]; 拷贝(堆栈、堆栈+容量、温度); 删除[]堆栈; 堆栈=温度; 容量*=2; } 堆栈[++顶部]=x; } 模板 void Stack::Pop(){ if(Isempty()) 抛出“堆栈为空”; 堆栈[top-->.~T(); } 结构偏移量{ INTA,b; }; 枚举方向{N,NE,E,SE,S,SW,W,NW}; 结构项{ int x,y,dir; 项目(){}; 项目(内部a、内部b、内部d){ x=a; y=b; dir=d; } }; 模板C++ 架构x86_64的未定义符号(我发现了错误,但无法修复。),c++,ostream,abstract-data-type,C++,Ostream,Abstract Data Type,我知道错误来自哪里。我已经在C++代码下面标记了它。 我不知道这个错误。希望有人能帮助我。如何解决这个问题 错误是: 架构x86_64的未定义符号: “运算符0”; 堆栈=新的T[容量]; top=-1; } 模板 内联布尔堆栈::Isempty()常量{ 返回顶部==-1; } 模板 内联T&Stack::Top()常量{ if(Isempty()) 抛出“堆栈为空”; 返回堆栈[顶部]; } 模板 无效堆栈::推送(常量T&x){ 如果(顶部==容量-1) { T*温度=新的T[2*容量];
ostream&operator运算符
的好友声明这是一个带有好友的模板。问题在于编译器将友元声明视为非模板,但它是一个模板。如果您坚持使用operator的类外定义,那么在main()
中引用的名为maze
的神秘对象是什么?它没有在任何地方申报。这是产生编译错误的真实代码,还是修改/更改的代码?将maze
替换为mark
后,显示的代码会产生许多其他编译错误。请修复您的问题,以便显示的代码只生成您所询问的编译错误。我在这里得到不同的错误:@SamVarshavchik如果我推送所有代码,它将太长。在这种情况下,您需要花费一些时间将代码简化为一个较小的示例,以再现您的确切错误。我已经输入了完整的代码。谢谢。我需要时间来理解它。如果我仍然有问题,希望您能帮助我。谢谢。我需要时间来理解它。如果我仍然有问题,希望你能帮助我。
#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);
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>&) {
//...
}