C++ 架构x86_64的未定义符号,链接器命令失败,退出代码为1
我有一个“架构x86_64的未定义符号”,似乎无法理解原因。下面是头文件:C++ 架构x86_64的未定义符号,链接器命令失败,退出代码为1,c++,xcode,architecture,linker-errors,avl-tree,C++,Xcode,Architecture,Linker Errors,Avl Tree,我有一个“架构x86_64的未定义符号”,似乎无法理解原因。下面是头文件: #include <string> #include <iostream> #include <iomanip> #include "assert.h" using namespace std; template <class Etype> class AvlNode { public: Etype element; AvlNode *parent;
#include <string>
#include <iostream>
#include <iomanip>
#include "assert.h"
using namespace std;
template <class Etype>
class AvlNode {
public:
Etype element;
AvlNode *parent;
AvlNode *left;
AvlNode *right;
int height;
AvlNode(Etype e,AvlNode *lt, AvlNode *rt, AvlNode *p,int h=0)
: element(e), left(lt), right(rt), parent(p), height(h) {}
};
template <class Etype>
class AvlTree {
public:
AvlTree() {root=NULL;}
~AvlTree() {makeEmpty();}
void makeEmpty() {makeEmpty(root);}
bool remove(Etype x) {return remove(root,x);}
void insert(Etype x) {return insert(x,root,NULL);}
bool tooHeavyLeft(AvlNode<Etype> * t);
bool tooHeavyRight(AvlNode<Etype> * t);
bool heavyRight(AvlNode<Etype> * t);
bool heavyLeft(AvlNode<Etype> * t);
protected:
AvlNode<Etype> *root;
void makeEmpty(AvlNode<Etype> *& t);
int height(AvlNode<Etype> *t);
bool remove(AvlNode<Etype> *& t,Etype word);
void insert(Etype x,AvlNode<Etype> *& t,AvlNode<Etype> *prev);
void rotateWithLeftChild(AvlNode<Etype> *& t);
void rorateWithRightChild(AvlNode<Etype> *& t);
void doubleWithLeftChild(AvlNode<Etype> *& t);
void doubleWithRightChild(AvlNode<Etype> *& t);
};
#包括
#包括
#包括
#包括“assert.h”
使用名称空间std;
样板
类AvlNode{
公众:
词缀元素;
AvlNode*父节点;
AvlNode*左;
AvlNode*右;
内部高度;
AvlNode(词组e,AvlNode*lt,AvlNode*rt,AvlNode*p,int h=0)
:元素(e)、左(lt)、右(rt)、父元素(p)、高度(h){
};
样板
类AvlTree{
公众:
AvlTree(){root=NULL;}
~AvlTree(){makeEmpty();}
void makeEmpty(){makeEmpty(root);}
bool remove(Etype x){返回remove(root,x);}
void insert(Etype x){返回insert(x,root,NULL);}
bool-tooHeavyLeft(AvlNode*t);
bool-tooHeavyRight(AvlNode*t);
bool-heavyRight(AvlNode*t);
bool heavyLeft(AvlNode*t);
受保护的:
AvlNode*根;
void makeEmpty(AvlNode*&t);
内部高度(AvlNode*t);
bool-remove(AvlNode*&t,Etype-word);
无效插入(词组x、AvlNode*&t、AvlNode*上一个);
void rotateWithLeftChild(AvlNode*&t);
void rorateWithRightChild(AvlNode*&t);
void doubleWithLeftChild(AvlNode*&t);
void doubleWithRightChild(AvlNode*&t);
};
这是源文件:
#include "AvlTree.h"
template <class Etype>
void AvlTree<Etype>::makeEmpty(AvlNode<Etype> *& t) {
if(t!=NULL) {
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t=NULL;
}
template <class Etype>
void AvlTree<Etype>::rotateWithLeftChild(AvlNode<Etype> *&t) {
assert(t!=NULL && t->left !=NULL);
AvlNode<Etype> *temp = t->left;
t->left = temp->right;
temp->right = t;
t->height = max( height( t->left ), height( t->right ) ) + 1;
temp->height = max( height( temp->left ), temp->height ) + 1;
t = temp;
}
template <class Etype>
int AvlTree<Etype>::height(AvlNode<Etype> *t) {
return t==NULL ? -1 : t->height;
}
#包括“AvlTree.h”
样板
void AvlTree::makeEmpty(AvlNode*&t){
如果(t!=NULL){
makeEmpty(t->左);
makeEmpty(t->右);
删除t;
}
t=零;
}
样板
void AvlTree::rotateWithLeftChild(AvlNode*&t){
断言(t!=NULL&&t->left!=NULL);
AvlNode*temp=t->左;
t->左=温度->右;
温度->右=t;
t->height=max(高度(t->left),高度(t->right))+1;
温度->高度=最大值(高度(温度->左侧),温度->高度)+1;
t=温度;
}
样板
int-AvlTree::高度(AvlNode*t){
返回t==NULL?-1:t->高度;
}
下面是我得到的错误:
Undefined symbols for architecture x86_64:
"AvlTree<int>::makeEmpty(AvlNode<int>*&)", referenced from:
AvlTree<int>::makeEmpty() 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)
架构x86_64的未定义符号:
“AvlTree::makeEmpty(AvlNode*&)”,引用自:
main.o中的AvlTree::makeEmpty()
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)
你能找出问题所在吗
谢谢
编辑:
我只是将源文件的内容复制到头文件,然后编译项目。这太好了,但是,如果有人向我解释了错误的原因,我将非常感激,因为我不知道。错误的原因是您应该始终将所有模板代码放在头文件中。将AvlTree.cpp中的所有代码移到AvlTree.h(并使函数内联)。删除AvlTree.cpp。链接器无法链接模板代码,它必须位于头文件中,以便编译器可以查看定义。请参阅以获取解释。错误的原因是您应该始终将所有模板代码放在头文件中。将AvlTree.cpp中的所有代码移到AvlTree.h(并使函数内联)。删除AvlTree.cpp。链接器无法链接模板代码,它必须位于头文件中,以便编译器可以查看定义。请参阅以获取解释