2015-09-06 37 views
0

我新的代码块的错误。我试图找到一个BST的高度,并使用max()函数。但是在编译的代码块抛出error.I正在使用Windows machine.I知道手动连接maths.h将解决issue.I知道如何链接math.h中使用gcc手动,但我不知道windows.Basically GCC相当于我想知道除了链接之外,还有其他解决方案吗?如果手动链接是唯一的选择,那么在CodeBlocks的Windows上如何做到这一点。谢谢!我是相当新的代码blocks.I已经花了太多时间在这个错误。可有人指出什么是我在这段代码

未定义引用最多

我敢肯定存在逻辑没有错误的所有import语句是正确的了。但仍然没有输出!下面是代码:

#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 

//Definition of Node for Binary search tree 
struct BstNode { 
    int data; 
    struct BstNode* left; 
    struct BstNode* right; 
}; 
struct BstNode* GetNewNode(int); 
struct BstNode* Insert(struct BstNode*,int); 
int findHeight(struct BstNode *); 
//struct BstNode* findMin(struct BstNode* root) 
// Function to create a new Node in heap 
struct BstNode* GetNewNode(int data) { 
    struct BstNode* newNode = (struct BstNode*)malloc(sizeof(struct BstNode)); 
    newNode->data = data; 
    newNode->left = newNode->right = NULL; 
    return newNode; 
} 

// To insert data in BST, returns address of root node 
struct BstNode* Insert(struct BstNode* root,int data) {//pass by value therefore * used and reurn stmt.otherwise pbarg ** no return 
    if(root == NULL) { // empty tree 
     root = GetNewNode(data); 
    } 
    // if data to be inserted is lesser, insert in left subtree. 
    else if(data <= root->data) { 
     root->left = Insert(root->left,data); 
    } 
    // else, insert in right subtree. 
    else { 
     root->right = Insert(root->right,data); 
    } 
    return root; 
} 
int findHeight(struct BstNode * root){ 
    if(root=NULL){ 
     return -1; 
    } 
    else{ 
     return max(findHeight(root->left),findHeight(root->right))+1; 
    } 

}; 
//BstNode* findMin(BstNode* root) 
int main() { 
    int a,b; 
    struct BstNode* root = NULL; // Creating an empty tree 
    /*Code to test the logic*/ 
    root = Insert(root,15); 
    root = Insert(root,10); 
    root = Insert(root,20); 
    root = Insert(root,25); 
    root = Insert(root,8); 
    root = Insert(root,12); 
findHeight(root); 
} 
+0

是什么让你的错误?的 – ThunderWiring

+1

可能重复的[C - 未定义参照SQRT(或其他数学函数)](http://stackoverflow.com/questions/5248919/c-undefined-reference-to-sqrt-or-other-mathematical-functions) – aghidini

+1

你在哪里听说过max()函数?有没有这样的事情在'math.h' – jbm

回答

0
  1. 你没有申报max功能。
  2. findHeight功能你做了if(root=NULL)应该是if(root==NULL)。像这样做:

    int findHeight(struct BstNode * root){ 
        if(root==NULL){ 
         return -1; 
        } else { 
         return max(findHeight(root->left),findHeight(root->right))+1; 
        } 
    } 
    
  3. 不要忘了打印findHeight的价值。

+1

Worked !! !!这么愚蠢的错误。我在Python上工作后回到了C,结果全部搞砸了。我认为max()也是在C中的函数中构建的。谢谢!所有这一次,我认为问题是在递归。没有想到这将是一个赋值operator.Thanks再次!!!! – user3847870

+0

我可以理解你的感觉,因为在我花了很多时间和** R **之后,当我回到** C++ **时,它也发生在我身上。 – surajsn

相关问题