4

I tried to look at other related posts but I'm sill stuck

My header files looks something like this

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

and

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

My implementation for queue looks like

#include<iostream>
using namespace std;

#include "Queue.hpp"
#include<cstdio>
#include<cstdlib>

And main looks like

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

I'm getting the following errors

 /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

What am I doing wrong here?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
rose
  • 657
  • 2
  • 10
  • 20
  • You could try removing this `using namespace std;` https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice or at least put it inside your header guards *after* all your `#includes` – Galik Feb 23 '16 at 07:05

5 Answers5

6

Don't define global variables in header file, sperate the declaration and definition to header and implmentation file. Such as,

In header file (Node.hpp)

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

In implmentation file (I think it's better to make a Node.cpp here)

Node *start;
Node *end;
int count = 0;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Thanks.. That seems to be giving me a whole bunch or errors, including Queue.hpp:17: error: ‘start’ was not declared in this scope Queue.hpp:18: error: ‘end’ was not declared in this scope – rose Feb 23 '16 at 07:15
  • @rose Did you include `Node.hpp` or `Queue.hpp` in `NewNode.hpp`? – songyuanyao Feb 23 '16 at 07:18
  • I did. Queue.hpp is actually the same as NewNode.hpp. I changed the name before posting it on here.. Right where I included Node *start; Node *end;.... I'm getting these errors ...Node.hpp:15: error: expected initializer before ‘*’ token Node.hpp:16: error: expected initializer before ‘*’ token – rose Feb 23 '16 at 07:30
  • @rose How did you write them? I tried a simplified demo [here](http://rextester.com/VHSS61364). It might be another question. – songyuanyao Feb 23 '16 at 07:43
  • `head` and `tail`? Have you changed the variable's name? – songyuanyao Feb 23 '16 at 07:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104244/discussion-between-rose-and-songyuanyao). – rose Feb 23 '16 at 07:54
2

Others already have explained the cause of the error: you are defining the same global variables in multiple translation units.

A possible fix is to define them in just one point and declare them as extern in the header file.

However, in my opinion the real question is: do you really need those global variables? If they are meant to be part of the state of a queue object, we should make them instance variables.

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; // <----
    };

In this way we can use multiple queue objects at runtime, and each of them will manage its own data. By comparison, instantiating many objects of the original class is useless, since they will all operate on the same queue.

TL;DR: encapsulate your state, avoid globals.

chi
  • 111,837
  • 3
  • 133
  • 218
  • thank you! That works so much better.. I guess I don't need them to be global. Thank you!! – rose Feb 23 '16 at 15:23
1

You define the variables start, end and count in the header files. That means every source file that includes that header file will have those variables defined in its translation unit

You should only declare them in the header file if you need to have those variables global, and then define them in a single source file.

To declare a variable in a header file you mark the variables as extern:

extern struct Node *start, *end;
extern int count;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You are defining start , count and end both in your header and main file which is causing the multiple definition error.

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
0
***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;
}


------------------------------------------------------------------------
  • Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. Repeating the question inside your source code as comment is definitely not an explanation. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Aug 04 '18 at 11:19