2012-05-06 3 views
1

AVL 트리에 삽입을 구현하는 임무를 수행 중이며 세 개의 "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"입니다 : 오류 중 하나가이 이유에 대한, 특히 궁금

#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

관련 문제