0

I'm working on a drum pad to learn C++ programming.

I'm using a class called DrumSensor which I need to create 5 times in an array.

I'm using a header file "settings.h" to store variables I'll use throughout my code.

settings.h

extern DrumSensor sensor[5];

settings.cpp

#include "settings.h"
DrumSensor sensor[5];

I've been experiencing a lot with this global object array, but I've never been able to compile it.

I tried to find references such as:

Creating Array of Objects c++

c++ global object

The problem is, no matter how I try to declare my "DrumSensors", I get the following error: ... multiple definition of ...

You can take a look at the code here: https://github.com/woodencase01/DrumSensor

  • This question seems to be relevant to your case, https://stackoverflow.com/questions/9196801/how-to-define-an-array-of-strings-of-characters-in-header-file – MaxPlankton Nov 09 '18 at 03:36
  • 1
    Please post the *minimal* code required to reproduce your problem. comments about the author name, etc are clearly not required to reproduce your problem. [mcve] also, `...` is not valid c++. You also need to post the full error message, not just part. And in this case we need to see all your commands you're using to compile this -- somehow you're linking settings.cpp twice. – xaxxon Nov 09 '18 at 04:14

1 Answers1

3

About the code in your question

The way you have shown it is correct. You declared it in a header (and therefore, by extension, in any source file including that header), and defined it once in a source file.

You must be accidentally linking settings.cpp twice, or accidentally including settings.cpp somewhere, or you accidentally wrote another definition for this array somewhere.


About the code you pointed us to

FWIW, in the GitHub project you linked to, you do not have a settings.cpp, just a settings.h with loads of objects defined in it (i.e. without extern). So the problem may simply be that the code you are building is not the same code that you've been talking about.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks for your help! Turns out I forgot to declare variables in the settings.cpp that were also "extern". About the Git repo, my bad, totally forgot a last push! – woodencase01 Nov 10 '18 at 14:07