Help with coding error [Solved]

Hello all! I got this error message due to the line of code listed below, but I don’t know how to resolve it.

Bubble_Trouble:64:92: error: invalid types '<unresolved overloaded function type>[uint8_t {aka unsigned char}]' for array subscript

       arduboy.drawBitmap(bubbles[y][0], bubbles[y][1], sprite[((18/(bubbles[y][2]-bubbles[y][0]))-1)], 8, 8, WHITE);

Any help would be much appreciated. The rest of the code is also below for any needed context, of course.

#include <Arduboy2.h>
Arduboy2Base arduboy;

const unsigned char PROGMEM sprite[3][8]
{
  //Big Bubble
  {60, 66, 129, 133, 133, 137, 66, 60},
  //Medium Bubble
  {8, 18, 33, 37, 26, 8, 0, 0},
  //Small Bubble
  {6, 9, 11, 6, 0, 0, 0, 0},
};

uint8_t GameState = 0;

uint8_t bubbles[20][8]
{
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
};



struct Draw
{

  void bubbles()
  {
    for (uint8_t y = 0; y < 20; ++y)
    {
      arduboy.drawBitmap(bubbles[y][0], bubbles[y][1], sprite[((18/(bubbles[y][2]-bubbles[y][0]))-1)], 8, 8, WHITE);
    }
  }

  
};



struct Function
{

  void moveBubbles()
  {
    
    for (uint8_t y = 0; y < 20; ++y)
    {
      if (bubbles[y][1] < 127) 
      {
        uint8_t bubbleSize=random(2, 5)*2;
        bubbles[y][0]=random(1, 22);
        bubbles[y][1]=0;
        bubbles[y][2]=bubbles[y][0]+bubbleSize;
        bubbles[y][3]=0;
        bubbles[y][4]=random(1, 22);
        bubbles[y][5]=bubbleSize;
        bubbles[y][6]=bubbles[y][0]+bubbleSize;
        bubbles[y][7]=bubbleSize;
      }
      else for (uint8_t x = 1; x < 7; x+2)
      {
        bubbles[y][x]+((18/(bubbles[y][2]-bubbles[y][0]))-1);
      }
        
    }
  }

  void createBubbles()
  {
    for (uint8_t y = 0; y < 20; ++y) 
    {
      uint8_t bubbleSize=random(2, 5)*2;
      bubbles[y][0]=random(1, 22);
      bubbles[y][1]=0;
      bubbles[y][2]=bubbles[y][0]+bubbleSize;
      bubbles[y][3]=0;
      bubbles[y][4]=random(1, 22);
      bubbles[y][5]=bubbleSize;
      bubbles[y][6]=bubbles[y][0]+bubbleSize;
      bubbles[y][7]=bubbleSize;
    }
  }
  
};



Draw draw;
Function function;



void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(60);
}



void gameloop()
{
  switch(GameState)
  {
  case 0:
    function.createBubbles();
  break;
    
  case 1:
    function.moveBubbles();
    draw.bubbles();
  break;
  }
}



void loop()
{
  if(!arduboy.nextFrame())
    return;
  arduboy.pollButtons();
  arduboy.clear();
  gameloop();
  arduboy.display();
}

You have an array named bubbles and also a member function of struct Draw named bubbles. Change the name of one or the other.

2 Likes

Thank you! I didn’t even think of that since I thought that the two wouldn’t at all interfer with eachother due to them being different and one being contained within a struct.

Ordinarily they might not but struct Draw thought that your references to bubbles within it were referencing its bubbles member function. If you really wanted them to both have the same name, you could put array bubbles in a namespace and use the scope operator (::) to access it from within Draw.

1 Like

It doesn’t even have to be in a namespace per se, simply using the :: operator with nothing on the left hand side is enough to specify that the global bubbles was the intended target.

That said…

@Revlis your structs don’t appear to be serving any purpose at the moment. Unless you’re planning to add member variables to them, you’d probably be better off just making your functions global, and possibly renaming your bubbles function to drawBubbles.

If the intent is just to group the functions then use a namespace as @MLXXXp mentioned.
There’s a decent tutorial discussing naming collisions and namespaces here:
https://www.learncpp.com/cpp-tutorial/2-9-naming-collisions-and-an-introduction-to-namespaces/

2 Likes