体系结构x86_64的未定义符号,链接器命令失败,退出代码为 1
Undefined symbols for architecture x86_64, linker command failed with exit code 1
我有一个"建筑x86_64的未定义符号",似乎无法理解原因。这是头文件:
#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);
};
这是源文件:
#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;
}
这是我得到的错误:
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)
你能找出问题所在吗?
谢谢
编辑:我只是将源文件的内容复制到头文件并编译了项目。这很好,但是,如果有人向我解释该错误的原因,我将不胜感激,因为我不知道。
错误的原因是应始终将所有模板代码放在头文件中。将您在 AvlTree 中的所有代码.cpp移动到 AvlTree.h(并使函数内联)。删除 AvlTree.cpp。链接器不能链接模板代码,它必须位于头文件中,以便编译器可以看到定义。有关解释,请参阅此处。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- Xcode:链接器命令失败,退出代码为 1(使用 -v 查看调用)[C++]
- 电报上的构建命令失败
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- exteranl原生构建问题,构建命令失败,android工作室
- build命令失败,外部原生问题android工作室
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- make:致命错误:目标命令失败
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- Android Studio 链接器命令在包含 Boost 库时失败
- Android opencv 3.4.1 Build 命令失败.未定义的引用
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- mkdir 从命令行工作,但在使用 NFS 时C++失败
- Xcode 10.0:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- C++使用命令行输入 txt 文件,但打开文件失败