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

Binary Tree Will Not Compile: Error 'WinMain@16'

本文关键字:WinMain@16 错误 编译 二叉树      更新时间:2023-10-16

我正在使用链表创建一个二叉树,下面我有我的类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 searchedn";
    cin>>num;
    if(Search(root, num) == true)
        cout<<"Foundn";
    else
        cout<<"Not Foundn";
}

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

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 searchedn";
    cin>>number;
    if(Search(root, number) == true)
        cout<<"Foundn";
    else
        cout<<"Not Foundn";
    }

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

prob1:将结构节点清除为私有,但尝试在 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 searedn";
    cin>>num;
    cout<<num;
    if(tree.Search(root, num) == true)
        cout<<"Foundn";
    else
        cout<<"Not Foundn";
    return 0;
}

T main()更改为int main()...这应该可以消除错误。main() 总是返回 int...

您似乎正在将 main 方法放入模板类中。这是行不通的。您需要将 main 函数设置为非成员函数。它返回int,通常为:

int main();

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

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