c++ - 已在 ConsoleApplication1.obj 中定义,
问题描述
我已经尝试过这个解决方案,但我无法理解我的情况。
我的项目结构是
- ConsoleApplication1.cpp(主)
- BST.h
- BST.cpp
- 帮助.cpp
现在我的主要是
#include "BST.h"
#include "help.cpp"
int main()
{
BST b1;
b1.insertion(5);
b1.insertion(3);
b1.insertion(2);
b1.insertion(5);
b1.insertion(7);
b1.insertion(9);
cout << predecessor(b1, 9)->data;
我的 BST.h 是
#pragma once
#include <iostream>
using namespace std;
struct node {
int data;
node* leftchild, * rightchild, * parent;
};
class BST {
private:
node* root;
public:
BST();
node* rooter();
void insertion(int);
void outleftandright();
node* search(int x);
};
BST.cpp 是
#include "BST.h"
node* BST::search(int x)
{
if (x == root->data)
{
return root;
}
node* temp = root;
while (temp)
{
if (x < temp->data)
{
temp = temp->leftchild;
}
else if (x > temp->data)
{
temp = temp->rightchild;
}
else if (x == temp->data)
{
return temp;
}
}
}
void BST::outleftandright() {
node* temp = root;
cout << "LEFT\n";
while (temp)
{
cout << temp->data << endl;
temp = temp->leftchild;
}
temp = root;
cout << "Right\n";
while (temp)
{
cout << temp->data << endl;
temp = temp->rightchild;
}
}
BST::BST()
{
root = nullptr;
}
node* BST::rooter()
{
return root;
}
void BST::insertion(int data)
{
if (root == nullptr)
{
root = new node;
root->data = data;
root->leftchild = nullptr;
root->rightchild = nullptr;
}
else {
node* temp = root;
while (true)
{
if (data <= temp->data)
{
if (temp->leftchild == nullptr)
{
temp->leftchild = new node;
temp->leftchild->parent = temp;
temp = temp->leftchild;
temp->data = data;
temp->leftchild = nullptr;
temp->rightchild = nullptr;
break;
}
temp = temp->leftchild;
}
else if (data > temp->data)
{
if (temp->rightchild == nullptr)
{
temp->rightchild = new node;
temp->rightchild->parent = temp;
temp = temp->rightchild;
temp->data = data;
temp->leftchild = nullptr;
temp->rightchild = nullptr;
break;
}
temp = temp->rightchild;
}
}
temp->data = data;
temp->leftchild = nullptr;
temp->rightchild = nullptr;
}
}
和 help.cpp 是
#include "BST.h"
node* minimum(node* b1)
{
while (b1->leftchild != nullptr)
{
b1 = b1->leftchild;
}
return b1;
}
node* maximum(node* temp)
{
while (temp->rightchild != nullptr)
{
temp = temp->rightchild;
}
return temp;
}
node* successor(BST b1, int x)
{
node* temproot = b1.search(x);
node* temproot1 = temproot;
if (temproot->rightchild != nullptr)
{
return minimum(temproot->rightchild);
}
else
{
node* y = temproot->parent;
while (y != nullptr && temproot == y->rightchild)
{
temproot = y;
y = temproot->parent;
}
if (y == nullptr)
{
return maximum(temproot1);
}
else {
return y;
}
}
}
node* predecessor(BST b1, int x)
{
node* temproot = b1.search(x);
node* temproot1 = temproot;
if (temproot->leftchild != nullptr)
{
return maximum(temproot->leftchild);
}
else
{
node* y = temproot->parent;
while (y != nullptr && temproot == y->leftchild)
{
temproot = y;
y = temproot->parent;
}
if (y == nullptr)
{
return minimum(temproot1);
}
else {
return y;
}
}
}
现在当我没有 help.cpp 并且 help.cpp 中的函数在我的 Consol1Application1.cpp 中时,它工作正常,但现在它给出了错误
1>help.obj : error LNK2005: "struct node * __cdecl maximum(struct node *)" (?maximum@@YAPEAUnode@@PEAU1@@Z) already defined in ConsoleApplication1.obj
1>help.obj : error LNK2005: "struct node * __cdecl minimum(struct node *)" (?minimum@@YAPEAUnode@@PEAU1@@Z) already defined in ConsoleApplication1.obj
1>help.obj : error LNK2005: "struct node * __cdecl predecessor(class BST,int)" (?predecessor@@YAPEAUnode@@VBST@@H@Z) already defined in ConsoleApplication1.obj
1>help.obj : error LNK2005: "struct node * __cdecl successor(class BST,int)" (?successor@@YAPEAUnode@@VBST@@H@Z) already defined in ConsoleApplication1.obj
1>C:\Users\Ahmad Mustafa Anis\source\repos\Week 13 Assign\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe : fatal error LNK1169: one or more multiply defined symbols found
如何处理?谢谢
解决方案
你不应该#include "help.cpp"
。这将创建函数的定义main.obj
。然后,当您将其与help.obj
(可能是)链接时,您会得到重复的定义。
相反,您应该创建help.h
仅包含来自help.cpp
. 然后使用
#include "help.h"
在其他文件中。
推荐阅读
- api - Viber 聊天 API,开发 Viber 聊天应用程序以支持深色主题
- azure - 使用 Azure AD 帐户将 Azure Logic App 连接到 SQL
- google-apps-script - 当单元格为空时,Google 表格应用脚本 onEdit 设置公式
- javascript - 如何使用本地存储存储最近提交的选项卡或刷新选项卡
- c# - 如何修改现有的 VB.NET 类以传递附加变量
- sql - 如何根据联接结果更新表?
- java - 为什么快速 java 文档的悬停不起作用?
- sql - 集成 Where、Group By 和 Order By 子句?
- c - 如何退出中断程序并返回主程序?
- json - 如何在 Elasticsearch 中按日期范围正确过滤条目?