2014-09-23 48 views
0

給出奇怪的鏈接錯誤我想用C++編寫簡單的二叉樹程序使用VS 2012.甚至所有的路徑設置它給我鏈接錯誤,如附件和當我評論在插入函數內註釋掉,它編譯時沒有錯誤。註釋功能正文在VS 2012

// C++ code 
#include "stdafx.h" 
#include <iostream> 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    cout <<" Simple Binary Tree Examples"; 

    getchar(); 

    return 0; 
} 

struct node 
{ 
    int data; 
    node *left; 
    node *right; 
}; 

public class BinaryTree 
{ 
    public : 
     BinaryTree(); 
     ~BinaryTree(); 

     void insert(int value); 
     /*{ 
     if(root==NULL) 
     { 
      insert(value,root); 
     } 
     else 
     { 
      root = new node; 
      root->data=value; 
     } 
     }*/ 

     void delete_tree(); 

    private: 
     node *root; 
     void insert(int value,node *leaf); 
}; 

BinaryTree::BinaryTree() 
{ 
    root=NULL; 
} 

BinaryTree::~BinaryTree() 
{ 
    delete_tree(); 
} 

void BinaryTree::insert(int value) 
{ 
    // If i un-comment the below code.. it gives link error. 
    /* if(root==NULL) 
    { 
     insert(value,root); 
    } 
    else 
    { 
     root = new node; 
     root->data=value; 
    }*/ 
} 

真的不知道什麼可能是錯誤的,因此共享整個代碼。

+0

嘗試新節點();這是有用的嗎? – yossico 2014-09-23 10:34:51

+0

@yossico - 新節點(); - 不工作。 – user1291401 2014-09-23 10:37:31

+1

雖然類定義說它應該存在,但我沒有看到爲delete_tree定義的任何函數體。 – 2014-09-23 10:39:22

回答

3

您的代碼不包含delete_tree的定義,它在析構函數中提及。

它不應該與該代碼評論,也不應與它編譯。

+0

瞭解了它..在爲delete_tree添加定義並插入(值,根)函數..它正確編譯..沒有錯誤。 – user1291401 2014-09-23 10:46:05