New to c++ classes and redeclaration

I am nearly positive this used to compile (like a few weeks ago). This is the repository (careful it has an error in it) and I am trying to fix the mistake, retest and update the code library. However now my test won’t compile… It seems like the compiler thinks I am redeclaring a bunch of stuff. Maybe I am… I am new to c++ so thanks to any help.

repository https://siliconjerome@bitbucket.org/siliconjerome/si_a2d_float.git

Instructions are you have to change si_a2d_float_test.h -> si_a2d_float_test.ino and then compile.

Not sure, if this will solve anything, but when you do your #ifndef check to avoid double include, the #endif should wrap the whole contents of the header file and not only the #define and the #include.

e.g. you do this

#ifndef APPLICATION_H
  #define APPLICATION_H
  #include "application.h"
#endif

but actually you can unconditionally do the #include since application.h does this


#ifndef APPLICATION_H_
#define APPLICATION_H_

// all the contents of the complete file

#endif

And for that reason the header itself prevents repeated inclusion and each of your header files should do the same.

Another thing (which is not necessary - see solution below) you can try with your .INO file is to add these two lines at the top

#pragma SPARK_NO_PREPROCESSOR
#include "application.h"

just in case the proprocesser contributes to your problem :wink:

And another tip is to look at the next few messages in your error output, since this usually also tells you with the “redeclare” error the position of the first declaration as a note


Solution of your compile error:

I’ve just built your code and the only thing I did was that I replaced this

#ifndef APPLICATION_H
  #define APPLICATION_H
  #include "application.h"
#endif

#ifndef SI_A2D_FLOAT_H
  #define SI_A2D_FLOAT_H
  #include "si_a2d_float.h"
#endif

#ifndef STDLIB_H
  #define STDLIB_H
  #include <stdlib.h>
#endif

with this

#include "si_a2d_float.h"
#include "stdlib.h"

But you should still apply the suggested form of the #ifndef check to ensure against double includes.

1 Like

Thanks @ScruffR I appreciate the advice. Here’s what I still don’t get…

This works (commented out top 2 lines)

//#pragma SPARK_NO_PREPROCESSOR
//#include "application.h"
#include "si_a2d_float.h"
#include <stdlib.h>

This doesn’t (delete first 2 lines) am I going insane?

#include "si_a2d_float.h"
#include <stdlib.h>

I think the preprocessor does not care for the comment characters and the #pragma is still used as if it was not commented.
You can try this be adding a syntax error which will make your build fail and then have a look at the warnings.

I would not use the <...> notation for the include.