'Adafruit_GFX_Button' was not declared in this scope

I've been stuck for days unable to resolve the compile error: 'Adafruit_GFX_Button' was not declared in this scope and would really appreciate ideas, suggestions, pointers to solutions or a recommended beer to relieve the stress. :wink:

This code attempting a function definition results in these error messages:

bool update_button(Adafruit_GFX_Button *b, bool down) {

error: ''Adafruit_GFX_Button' was not declared in this scope

error: 'b' was not declared in this scope

error: expected primary-expression before 'bool'

error: expression list treated as compound expression in initializer [-fpermissive]

In function 'bool update_button(Adafruit_GFX_Button*, bool)':
error: 'bool update_button(Adafruit_GFX_Button*, bool)' redeclared as different kind of symbol
bool update_button(Adafruit_GFX_Button *b, bool down) {

note: previous declaration 'bool update_button'

This code is in a tab all by itself. The second line successfully references Adafruit_GFX_Button but the third line and several other similar lines always throw errors.

#include "Adafruit_GFX.h"

Adafruit_GFX_Button circle_btn, square_btn, triangle_btn, line_btn, led_btn, RGB_btn;

bool update_button(Adafruit_GFX_Button *b, bool down) {
  b->press(down && b->contains(pixel_x, pixel_y));
  if (b->justReleased())
    b->drawButton(false);
  if (b->justPressed())
    b->drawButton(true);
  return down;
}

The same exact code in a simple project works great but when I add it into my main project it consistently gets these errors. It would seem something about the preprocessing may be the problem. But how to tease apart what????

You need to install the library Adafruit_GFX_Library. Add an include for it
#include "Adafruit_GFX_Library.h"
That library has the Button

1 Like

Thanks for the reply. Yes I've done that, note the include statement in the code I posted above. And note the declaration of circle_btn etc as an Adafruit_GFX_Button appears to work OK. But for some reason the compiler consistently complains whenever I try to define a function with an Adafruit_GFX_Button type parameter. I'm going to read up on function prototypes in case that might lead to a solution.

Check out include guards.

The problem may not be where you think it is. If you knew what the problem was then you wouldn't be asking here. So post a complete piece of code that someone can use to try to find your problem. Guessing from your snippet is a waste of time.

This (above) says your your code wants your library local, inside this project's folder/directory.

Move your library.

@JoeHuber
<> is system
"" is local followed by system. Used to override a system declare.
There is no indication the library can't be found, it appears (partial guess since complete code is missing) it's just a case of not being included in the right place probably because he does not know how to use include guards.. The clue is in the error message

redeclared as different kind of symbol

1 Like

Hi everyone, thanks for the ideas and suggestions.

I figured it out. I needed to use Function Prototypes in my first tab for the functions I would later define which have Adafruit_GFX_Button parameters. Apparently the automatic function prototype generation doesn't work in this case.

I had tried to create a smaller demo sketch but everything worked perfectly there. So I started this thread with the best info I could come up with, short of sending my entire project with 10 tabs that I'm sure no one was interested in investigating.

I've heard about occasionally needing to add my own Function Prototypes but this is the first instance they've actually been needed. Learned something new this week...

sonofcy You are correct that I have no idea how, if or when to use include guards. :wink:

See post #5 for information on organization. It sounds like you may have some misunderstandings.
Organization
Here is info re include guards

Include guards ensures that compiler will process this file only once, no matter how many times it is included. Include guards are just series of preprocessor directives that guarantees file will only be included once.
Preprocessors used:

  • #ifndef: if not defined, determines if provided macros does not exists.
  • #define: Defines the macros.
  • #endif: Closes off #ifndef directive.

The block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined.

Thanks for all the great info and further pointers. You're right that I'm not using traditional C style file organization. I only import third party library code and keep all of my own code in IDE tabs since I don't consider it as reusable in other projects. Thus I don't use .h nor .cpp files myself. I do try to keep things structured and modular so you might call my style as chunky soup, no spaghetti allowed! :slightly_smiling_face:

I suppose the include guard issue arose because I knowingly included the Adafruit library both in my first tab with all the other includes and as a test I also placed the same include in the same tab as the function definition that was causing the errors just to be certain is was included as close as possible. "Curses, it's right there, why can't you see it!"

I suppose the Adafruit library may have been missing the include guards. Now that I've added the function prototypes and everything is finally working again I've obviously removed the redundant local include and have only the one in the first tab.