首页 > 解决方案 > 已在 ConsoleApplication1.obj 中定义,

问题描述

我已经尝试过这个解决方案,但我无法理解我的情况。

我的项目结构是

现在我的主要是

#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

如何处理?谢谢

标签: c++ooplinker

解决方案


你不应该#include "help.cpp"。这将创建函数的定义main.obj。然后,当您将其与help.obj(可能是)链接时,您会得到重复的定义。

相反,您应该创建help.h仅包含来自help.cpp. 然后使用

#include "help.h"

在其他文件中。


推荐阅读