0
我在纯C中做了它,但现在我试图在C++中实现二叉搜索树。大部分代码是完全相同的,但不是malloc()
我想使用new
运算符。但是,我得到这个错误:未定义的符号...运算符new(无符号长整数)
Undefined symbols for architecture x86_64:
"operator new(unsigned long)", referenced from:
GetNewNode(int) in BstTreeV3-beb469.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的ideone代码。 Click here to view it.
Node* GetNewNode(int data) {
Node *newNode = new Node();
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void Insert(Node **root, int data)
{
if (*root == NULL) { // empty tree
*root = GetNewNode(data);
}
else if ((*root)->data < data) {
Insert(&((*root)->left), data);
}
else {
Insert(&((*root)->right), data);
}
}
我明白这可能不是最好的实现方法,但我只是在练习。如果您对如何实施BST有任何建议,请随时发表评论。
你的编译器是什么? –
如果你使用'new'(和'delete',我希望),那么你不是编程C,所以我删除了这个标签。这可能是你的链接失败的原因,因为你用'clang'构建了一个不与C++运行时库链接的应用程序,你应该用'clang ++'构建,而不是与C++运行时库自动链接(或者手动链接C++运行时库)。 –
Ahh是的,Sublime仍然使用C++构建系统,而不是使用基于源文件类型的C++自动生成。我想知道为什么不自动切换构建系统。我应该删除我原来的问题吗? – nodebase