【问题标题】:how to fix multiple definition error in c++?如何修复 C++ 中的多个定义错误?
【发布时间】:2016-02-23 07:02:11
【问题描述】:

我试图查看其他相关帖子,但我仍然卡住了

我的头文件看起来像这样

节点.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

我在这里做错了什么?

【问题讨论】:

标签: c++ definition


【解决方案1】:

不要在头文件中定义全局变量,将声明和定义分开到头文件和实现文件中。比如,

在头文件中(Node.hpp

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

在实现文件中(我认为最好在这里创建一个Node.cpp

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

【讨论】:

  • 谢谢.. 这似乎给了我一大堆错误,包括 Queue.hpp:17: error: 'start' is not declared in this scope Queue.hpp:18: error: ' end' 未在此范围内声明
  • @rose 你是否在NewNode.hpp 中加入了Node.hppQueue.hpp
  • 我做到了。 Queue.hpp 实际上与 NewNode.hpp 相同。我在这里发布之前更改了名称.. 就在我包含 Node start; 的地方。节点 *end;.... 我收到这些错误...Node.hpp:15: 错误: '' 令牌之前的预期初始化程序 Node.hpp:16: 错误: '*' 令牌之前的预期初始化程序
  • @rose 你是怎么写的?我尝试了一个简化的演示 here。这可能是另一个问题。
  • headtail?您是否更改了变量的名称?
【解决方案2】:

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

一种可能的解决方法是在一个点定义它们并在头文件中将它们声明为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:封装您的状态,避免使用全局变量。

【讨论】:

  • 谢谢!那效果好多了..我想我不需要它们是全球性的。谢谢!!
【解决方案3】:

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

如果您需要将这些变量设为全局变量,您应该只在头文件中声明它们,然后在单个源文件中定义它们。

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

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 您可以编辑原始答案,而不是发布新答案。
    • 对信誉超过 10k 的用户可见。
    【解决方案5】:
    ***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;
    }
    
    
    ------------------------------------------------------------------------
    

    【讨论】:

    • 欢迎来到 Stack Overflow!请不要只用源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。在源代码中重复问题作为注释绝对不是解释。请参阅:How do I write a good answer?。谢谢
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多