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

2
  • This question seems to be relevant to your case, stackoverflow.com/questions/9196801/… Commented Nov 9, 2018 at 3: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. minimal reproducible example 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. Commented Nov 9, 2018 at 4:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.