0

I made a new Arduino IDE project ABC, and am given ABC.ino. Then I create a XYZ.h and XYZ.cpp.


ABC.ino:

#include "XYZ.h"
void setup() {}
void loop() {}

XYZ.h:

#ifndef XYZ
#define XYZ
#include "Arduino.h"
class XYZ { };
#endif

XYZ.cpp:

#include "Arduino.h"
#include "XYZ.h"

When I try to compile this, I get some error:

In file included from sketch/XYZ.cpp:2:0:
XYZ:8: error: abstract declarator '<anonymous class>' used as declaration
 };
 ^
exit status 1
abstract declarator '<anonymous class>' used as declaration

However, when I rename my class to something other than the file name, like XYZa, the compilation succeeds.


Why can't I have the same name class as the file?

I've done C++ projects (independent of Arduino) via Xcode and nano, and this has never been a problem.

asked Dec 2, 2016 at 21:32
2
  • INCLUDE THE FULL CODE!!! Commented Dec 2, 2016 at 21:34
  • @DatHa -- This is the full code. It's extremely boilerplate. :) Commented Dec 2, 2016 at 21:35

2 Answers 2

2
#define XYZ
...
class XYZ { };

After preprocessing, what do you think that ends up as?

answered Dec 2, 2016 at 21:37
8
  • IDK. But, if you're suggesting that it's because it's an empty class, I think I've ruled it out. If you add void QRS() to .h and void XYZ::QRS() {} to .cpp I still get that error AND another one. Commented Dec 2, 2016 at 21:40
  • Do you know what #define XYZ actually does? Commented Dec 2, 2016 at 21:41
  • I think it (along with #ifndef and #endif) is suppose to keep from duplicate code during linking? Commented Dec 2, 2016 at 21:42
  • That is what it is being used for. I asked if you know what it does? Commented Dec 2, 2016 at 21:42
  • Not particularly. :) Commented Dec 2, 2016 at 21:43
3
#define XYZ
class XYZ { };

The first line tells the "C preprocessor" (called before C and C++ compilers) to replace XYZ with nothing everywhere it happens, hence after preprocessing, and before compiling, that code becomes:

#define XYZ
class { };

This is where C++ compiler complains. It has nothing to do with your file name.

Among C and C++ programmers, it is commonly admitted that header files should be protected by macros named after the file name including the extension:

#ifndef XYZ_H
#define XYZ_H

If you use this practice, you won't have this problem anymore.

answered Dec 2, 2016 at 21:47
1
  • That's what is inconsistent with my Arduino program and my other ones (Xcode/nano). I forgot to append the _H_ (or _H as you prefer). Commented Dec 2, 2016 at 21:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.