如何在c ++中修复多个定义错误?

ros*_*ose 4 c++ definition

我试着看看其他相关的帖子,但我已经陷入了困境

我的头文件看起来像这样

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
Run Code Online (Sandbox Code Playgroud)

Queue.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
Run Code Online (Sandbox Code Playgroud)

我的队列实现看起来像

#include<iostream>
using namespace std;

#include "Queue.hpp"
#include<cstdio>
#include<cstdlib>
Run Code Online (Sandbox Code Playgroud)

主要看起来像

  #include<iostream>
    using namespace std;
    #include "Queue.hpp"
    #include<cstdio>
    #include<cstdlib>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

 /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
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

son*_*yao 5

不要在头文件中定义全局变量,将声明和定义扩展到头文件和文件夹文件.如,

在头文件(Node.hpp)中

extern Node *start;
extern Node *end;
extern int count;
Run Code Online (Sandbox Code Playgroud)

在implmentation文件中(我认为最好在Node.cpp这里制作一个)

Node *start;
Node *end;
int count = 0;
Run Code Online (Sandbox Code Playgroud)