二叉树无法编译:错误'WinMain@16'
Binary Tree Will Not Compile: Error 'WinMain@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 程序。但您的代码似乎是一个控制台程序。
相关文章:
- Visual Studio Code "undefined reference to `WinMain@16'"
- "未定义对'WinMain@16'的引用"gcc 编辑器中的错误
- 我无法弄清楚"对Winmain@16错误的未定义引用"
- 使用 C++17 标准对"WinMain@16"的未定义引用
- 二叉树无法编译:错误'WinMain@16'
- 对'WinMain@16'的未定义引用 (C++)
- 尝试使用类文件,出现"undefined reference to `WinMain@16'"错误
- 未定义对“WinMain@16”的引用
- 对WinMain@16 - 代码块的未定义引用
- CodeBlocks未定义对的引用WinMain@16.
- 未定义对"WinMain@16"的引用?
- GNU GCC:尝试编译发行版时对"WinMain@16"的未定义引用(代码::Blocks)
- c++代码块错误:对SDL_PollEvent和WinMain@16的未定义引用
- 对Qt和C-Lion的"WinMain@16"的未定义引用
- SDL 2 对"WinMain@16"和多个 SDL 函数的未定义引用
- 在窗口"undefined reference to 'WinMain@16'"中创建窗口时出错
- c++中对WinMain@16的未定义引用
- 对"WinMain@16"的未定义引用
- c++ -对' WinMain@16. '的未定义引用
- block, MinGW, libsdl和GNU c++编译器:对' WinMain@16的未定义引用