二叉树无法编译:错误'WinMain @ 16'

时间:2014-02-27 07:08:47

标签: c++ compiler-errors linked-list binary-tree codeblocks

我正在使用链接列表创建二叉树,下面我有我的类funtree:

template <typename T>
class funtree
{
private:
struct Node
{
    T data;
    Node *left;
    Node *right;
};
Node* GetNewNode(T data)
{
    Node* newNode = new Node();
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;

}
Node* Insert(Node* root, T data)
{
    if(root == NULL) //empty tree
    {
        root = GetNewNode(data);
        return root;
    }
    else if(data <= root->data)
    {
        root->left = Insert(root->left, data);
    }
    else
    {
        root->right = Insert(root->left, data);
    }
    return root;
}
bool Search(Node* root, int data)
{
   if(root == NULL)
        return false;
   else if (root->data == data)
    return true;
   else if (data <= root->data)
    return Search(root->left, data);
   else
    return Search(root->right, data);

和我的主要方法:

 T main()
{
    Node* root; // pointer to root node
    root = NULL; // setting tree as empty
    Insert(root,15);
    Insert(root,10);
    Insert(root,20);
    T num;
    cout<<"Enter number to be searched\n";
    cin>>num;
    if(Search(root, num) == true)
        cout<<"Found\n";
    else
        cout<<"Not Found\n";
}

我正在尝试测试我的搜索方法以查看它是否有效以及何时尝试编译我只会收到错误:

main.c||undefined reference to `WinMain@16'|

该程序也是一个控制台应用程序,它们都在同一个类中,我不认为这些是问题,但以防万一需要知道。

编辑:

我已经更改了代码的某些部分,但会将其粘贴到我的编译器中,而不是将主函数和类分开:

#include <iostream>

using namespace std;

template <typename T>
class funtree
{
private:
struct Node
{
    T data;
    Node *left;
    Node *right;
};
Node* GetNewNode(T data)
{
    Node* newNode = new Node();
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;

}
Node* Insert(Node* root, T data)
{
    if(root == NULL) //empty tree
    {
        root = GetNewNode(data);
        return root;
    }
    else if(data <= root->data)
    {
        root->left = Insert(root->left, data);
    }
    else
    {
        root->right = Insert(root->left, data);
    }
    return root;
}
bool Search(Node* root, int data)
{
   if(root == NULL)
        return false;
   else if (root->data == data)
    return true;
   else if (data <= root->data)
    return Search(root->left, data);
   else
    return Search(root->right, data);

}

  int main()
    {
    Node* root; // pointer to root node
    root = NULL; // setting tree as empty
    Insert(root,15);
    Insert(root,10);
    Insert(root,20);
    T number;
    cout<<"Enter number to be searched\n";
    cin>>number;
    if(Search(root, number) == true)
        cout<<"Found\n";
    else
        cout<<"Not Found\n";
    }

3 个答案:

答案 0 :(得分:0)

T main()更改为int main() ...这应删除错误.... main()始终返回int ...

答案 1 :(得分:0)

您似乎将主要方法放入模板类中。那不行。您需要将main作为非成员函数。它返回int,通常是:

int main();

int main(int argc, char* argv[]);

您可能还需要更改编译,因为链接器期望WinMain通常表明它正在构建GUI程序。但是您的代码似乎是一个控制台程序。

答案 2 :(得分:0)

您的代码中存在很多问题,希望这段代码有帮助

prob1:declear struct Node为private,但试图在main

中访问它

prob2:root * as参数在您新建节点时更改,但您没有返回新根。要么将Insert定义为Node* Insert(Node*& root, T data),要么将函数的返回值赋值给root就可以了。

prob3:main应该总是返回int

#include <iostream>
using namespace std;
template <typename T>
class funtree
{
public:
struct Node
{
    T data;
    Node *left;
    Node *right;
};
Node* GetNewNode(T data)
{
    Node* newNode = new Node();
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;

}
Node* Insert(Node* root, T data)
{
    if(root == NULL) //empty tree
    {
        root = GetNewNode(data);
        return root;
    }
    else if(data <= root->data)
    {
        root->left = Insert(root->left, data);
    }
    else
    {
        root->right = Insert(root->left, data);
    }
    return root;
}
bool Search(Node* root, int data)
{
   if(root == NULL)
        return false;
   else if (root->data == data)
    return true;
   else if (data <= root->data)
    return Search(root->left, data);
   else
    return Search(root->right, data);
}
};
int main()
{
    funtree<int> tree;
    funtree<int>::Node* root; // pointer to root node
    root = NULL; // setting tree as empty
    root = tree.Insert(root,15);
    root = tree.Insert(root,10);
    root = tree.Insert(root,20);
    int num;
    cout<<"Enter number to be seared\n";
    cin>>num;
    cout<<num;
    if(tree.Search(root, num) == true)
        cout<<"Found\n";
    else
        cout<<"Not Found\n";
    return 0;
}