我正在进行一项将插入实现到AVL树的任务,我得到三个“Apple Mach-O Linker(ld)”错误。我对这些错误的有限理解是,当从未正确包含在项目中的库中引用某些内容时,它们会出现。以下是Xcode给我的错误消息:
Ld /Users/gusmurphy/Library/Developer/Xcode/DerivedData/AVLInsertion-eylpvolyxvuwbygcvocqhleyvzcv/Build/Products/Debug/AVLInsertion normal x86_64
cd /Users/gusmurphy/Documents/CMSC270/AVLInsertion
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/clang++ -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -L/Users/gusmurphy/Library/Developer/Xcode/DerivedData/AVLInsertion-eylpvolyxvuwbygcvocqhleyvzcv/Build/Products/Debug -F/Users/gusmurphy/Library/Developer/Xcode/DerivedData/AVLInsertion-eylpvolyxvuwbygcvocqhleyvzcv/Build/Products/Debug -filelist /Users/gusmurphy/Library/Developer/Xcode/DerivedData/AVLInsertion-eylpvolyxvuwbygcvocqhleyvzcv/Build/Intermediates/AVLInsertion.build/Debug/AVLInsertion.build/Objects-normal/x86_64/AVLInsertion.LinkFileList -mmacosx-version-min=10.6 -o /Users/gusmurphy/Library/Developer/Xcode/DerivedData/AVLInsertion-eylpvolyxvuwbygcvocqhleyvzcv/Build/Products/Debug/AVLInsertion
Undefined symbols for architecture x86_64:
"AVLTree<int>::insert(int const&)", referenced from:
_main in main.o
"AVLTree<int>::snapShot(std::ostream&, AVLNode<int>*)", referenced from:
AVLTree<int>::snapShot(std::ostream&) in main.o
"AVLTree<int>::clear(AVLNode<int>*)", referenced from:
AVLTree<int>::clear() 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)
这是“main.cpp”:
#include <iostream>
#include "AVLTree.h"
#include <stdio.h>
int main (int argc, const char * argv[])
{
cout << "Enter an integer to be insterted: " << endl;
int i;
AVLTree<int> tree;
while (true) {
scanf("%d", &i);
tree.insert(i);
tree.snapShot(cout);
}
}
我特别好奇的一件事是为什么其中一个错误与AVLTree<int>::clear(AVLNode<int>*)
有关,即使它没有明确调用(据我所知)。我还将AVLTree的头文件和实现文件放在这里。
这是“AVLTree.h”:
#include <iostream>
using namespace std;
#ifndef AVL_TREE
#define AVL_TREE
template<class T>
class AVLNode {
public:
AVLNode() {
left = right = 0;
}
AVLNode(const T& el, AVLNode *l = 0, AVLNode *r = 0, int b = 0) {
key = el; left = l; right = r; balance = b;
}
T key;
AVLNode *left, *right;
int balance;
};
template<class T>
class AVLTree {
public:
AVLTree() {
root = 0;
}
~AVLTree() {
clear();
}
void clear() {
clear(root); root = 0;
}
bool isEmpty() const {
return root == 0;
}
void snapShot(ostream& out) {
out << "TreeForm[";
snapShot(out,root);
out << ']' << endl;
}
void preorder() {
preorder(root);
}
void inorder() {
inorder(root);
}
void postorder() {
postorder(root);
}
void insert(const T&);
T* search(const T& el) const {
return search(root,el);
}
protected:
AVLNode<T>* root;
void clear(AVLNode<T>*);
T* search(AVLTree<T>* p, const T& el) const;
void preorder(AVLNode<T>*);
void inorder(AVLNode<T>*);
void postorder(AVLNode<T>*);
void snapShot(ostream& out,AVLNode<T> *p);
void visit(AVLNode<T>* p) {
cout << p->key << ' ';
}
void rightRotation(AVLNode<T>* &node) {
AVLNode<T>* left = node->left;
node->left = left->right;
left->right = node;
node = left;
}
void leftRotation(AVLNode<T>* &node) {
AVLNode<T>* right = node->right;
node->right = right->left;
right->left = node;
node = right;
}
};
#endif
和“AVLTree.cpp”:
#include "AVLTree.h"
#include <iostream>
template<class T>
void AVLTree<T>::clear(AVLNode<T> *p) {
if (p != 0) {
clear(p->left);
clear(p->right);
delete p;
}
}
template<class T>
void AVLTree<T>::insert(const T& el) {
if (root == 0) {
root = new AVLNode<T>(el);
return;
}
AVLNode<T>* parent = 0;
AVLNode<T>** tmp = root;
AVLNode<T>* ancestor = 0;
while (tmp != 0) {
parent = *tmp;
if (ancestor == 0 && (parent->balance == 1 || parent->balance == -1))
ancestor = parent;
// "ancestor" will point to the first node with an unnacceptable balance, if there is one.
if (el >= parent) {
tmp = parent->right;
parent->balance++;
} else {
tmp = parent->left;
parent->balance--;
}
};
tmp = new AVLNode<T>(el);
if (ancestor != 0) {
if (ancestor->balance != 0) {
if (el >= ancestor->key) {
if (el >= parent) {
rightRotation(ancestor);
} else {
rightRotation(parent);
rightRotation(ancestor);
}
} else {
if (el < parent) {
leftRotation(ancestor);
} else {
leftRotation(parent);
leftRotation(ancestor);
}
}
}
}
}
template<class T>
T* AVLTree<T>::search(AVLTree<T>* p, const T& el) const {
while (p != 0)
if (el == p->key)
return &p->key;
else if (el < p->key)
p = p->left;
else p = p->right;
return 0;
}
template<class T>
void AVLTree<T>::inorder(AVLNode<T> *p) {
if (p != 0) {
inorder(p->left);
visit(p);
inorder(p->right);
}
}
template<class T>
void AVLTree<T>::preorder(AVLNode<T> *p) {
if (p != 0) {
visit(p);
preorder(p->left);
preorder(p->right);
}
}
template<class T>
void AVLTree<T>::postorder(AVLNode<T>* p) {
if (p != 0) {
postorder(p->left);
postorder(p->right);
visit(p);
}
}
template <class T>
void AVLTree<T>::snapShot(ostream& out, AVLNode<T> *p)
{
out << '\"' << p->key << '\"';
if(p->left != 0 || p->right != 0) {
out << '[';
if(p->left==0)
out << "\"\"";
else
snapShot(out,p->left);
out << ',';
if(p->right==0)
out << "\"\"";
else
snapShot(out,p->right);
out << ']';
}
}
非常感谢你的时间!我很抱歉创建了另一个关于此错误消息的帖子,但我无法将我发现的问题实例放在一起。