2012-05-06 11 views
1

私はAVLツリーへの挿入を実装する課題に取り組んでおり、3つの "Apple Mach-O Linker(ld)"エラーが発生しています。これらのエラーの私の限られた理解は、何かがプロジェクトに適切に含まれていないライブラリから参照されるときに出てくるということです。ここで非常に簡単なプログラムでApple Mach-O Linker(ld)エラーが発生しました

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に」である:ここにいるのXcodeが私を与えているエラーメッセージでエラーのいずれかがしなければならない理由を私はおよそ特に興味

#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のヘッダーファイルと実装ファイルをここに配置します。

#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":ここでは

は "AVLTree.h" である

#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 << ']'; 
    } 
} 

はお時間をありがとうございました!そして、このエラーメッセージについて別のスレッドを作成して申し訳ありませんが、私が見つけたものから私の問題のインスタンスについて多くのことをまとめることはできませんでした。

答えて

3

テンプレートファイルのクラス定義がヘッダーファイルで使用可能である必要があります。これについてはhereを読むことができます。

+0

ありがとうございます!それはそれをクリアした。 – Gus

関連する問題