如何修复C ++中的多个定义错误

how to fix multiple definition error in c++?

本文关键字:定义 错误 何修复      更新时间:2023-10-16

>我试图查看其他相关帖子,但我被困住了

我的头文件看起来像这样

Node.hpp

      #include<iostream>
using namespace std;
#ifndef NODE_HPP
    #define NODE_HPP

        struct Node
        {
            int value;
             Node *start;
             Node *end;
        }
         *start, *end;
         int count= 0;

        #endif

队列.hpp

  #include<iostream>
using namespace std;
#ifndef QUEUE_HPP
#define QUEUE_HPP
#include "Node.hpp"
class Queue{
    public:
        Node *nNode(int value);
        void add(int value);
        void remove();
        void display();
        void firstItem();
        Queue()
        {   
            start = NULL;
            end = NULL;
        }   
    };
    #endif

我的队列实现看起来像

#include<iostream>
using namespace std;
#include "Queue.hpp"
#include<cstdio>
#include<cstdlib>

主要看起来像

  #include<iostream>
    using namespace std;
    #include "Queue.hpp"
    #include<cstdio>
    #include<cstdlib>

我收到以下错误

 /tmp/ccPGEDzG.o:(.bss+0x0): multiple definition of `start'
    /tmp/ccJSCU8M.o:(.bss+0x0): first defined here
    /tmp/ccPGEDzG.o:(.bss+0x8): multiple definition of `end'
    /tmp/ccJSCU8M.o:(.bss+0x8): first defined here
    /tmp/ccPGEDzG.o:(.bss+0x10): multiple definition of `count'
    /tmp/ccJSCU8M.o:(.bss+0x10): first defined here

我在这里做错了什么?

不要在头文件中定义全局变量,将声明和定义拆分为头文件和嵌入文件。如

在头文件 ( Node.hpp

extern Node *start;
extern Node *end;
extern int count;

在安装文件中(我认为最好在这里做一个Node.cpp

Node *start;
Node *end;
int count = 0;

其他人已经解释了错误的原因:您在多个翻译单元中定义相同的全局变量。

可能的解决方法是仅在一个点中定义它们,并在头文件中将它们声明为extern

然而,在我看来,真正的问题是:你真的需要这些全局变量吗?如果它们是队列对象状态的一部分,我们应该使它们成为实例变量。

class Queue{
    public:
        Node *nNode(int value);
        void add(int value);
        void remove();
        void display();
        void firstItem();
        Queue()
        {   
            start = NULL;
            end = NULL;
        }   
        Node *start, *end; // <----
    };

通过这种方式,我们可以在运行时使用多个队列对象,每个对象都将管理自己的数据。相比之下,实例化原始类的许多对象是无用的,因为它们都将在同一个队列上运行。

TL;DR:封装你的状态,避免全局。

在头文件中定义变量 startendcount。这意味着包含该头文件的每个源文件都将在其翻译单元中定义这些变量

仅当需要将这些变量全局化时,才应在头文件中声明它们,然后在单个源文件中定义它们。

要在头文件中声明变量,请将变量标记为extern

extern struct Node *start, *end;
extern int count;

您在头文件和主文件中定义了startcountend,这导致了多重定义错误。

***how to fix multiple definition of 'dictionaryArrayAdd' error in c*** ?

typedef struct{
    int prefix; // prefix for byte > 255
    int character; // the last byte of the string
} DictElement;
void dictionaryArrayAdd(int prefix, int character, int value);
int dictionaryArrayPrefix(int value);
int dictionaryArrayCharacter(int value);
DictElement dictionaryArray[4095];
// add prefix + character to the dictionary`enter code here`
void dictionaryArrayAdd(int prefix, int character, int value) {
    dictionaryArray[value].prefix = prefix;
    dictionaryArray[value].character = character;
}
int dictionaryArrayPrefix(int value) {
    return dictionaryArray[value].prefix;
}
int dictionaryArrayCharacter(int value) {
    return dictionaryArray[value].character;
}

------------------------------------------------------------------------