user61967: I am trying to create a basic snake game using the arduino UNO along with the MAX72XX module, the LCD module and the analog joystick module. I am wanting to program my program using object orientated programming. I have no experience with C++ so I am struggling quite a bit to understand what the following error is trying to say to me: request for member 'following_direction' in 'snake[0]', which is of non-class type 'int'.Here is the full code:
#include "LedControl.h"
#include <LiquidCrystal.h>
LedControl lc=LedControl(12,10,11,1);
LiquidCrystal lcd(8, 13, 3, 5, 6, 7);


/*lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);*/

class Snake_body
{
  //private:




  public:

    int following_direction;
    int body_length[64];
    int x;
    int y;
    int previous_x;
    int previous_y;

    Snake_body(int x_value, int y_value) 
    {
      this->following_direction = 1;
      this->x = x_value;
      this->y = y_value;
      this->previous_y = y_value;
      this->previous_x = x_value;
    }
    /*void configure(int x_value, int y_value)
    {
      this->following_direction = 1;
      this->x = x_value;
      this->y = y_value;
      this->previous_y = y_value;
      this->previous_x = x_value;  
    }*/

};


class Power_up
{
  private:

  public:
    virtual void init() 
    {
      this->x = 0;
      this->y = 0;
    }

    int x;
    int y;
};

int snake[64];
int snakes_length;


Snake_body *snake_body;
Power_up *power_up;



void setup() {

  lcd.begin(16, 1);

  snakes_length = 1;

  for(int z = 1; z < 64; z++)
  {
  snake[z] = 0;
  }

  snake[0] = new Snake_body(0,0);
  //Snake_body snake[0] = new Snake_body();
  //snake[0].configure(0,0);
  // put your setup code here, to run once:
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);

  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  Serial.begin(9600);
  power_up->x = random(0, 8);
  power_up->y = random(0, 8);
}

int snake_direction;


void loop() 
{
  int x_axis = analogRead(A0);
  int y_axis = analogRead(A1);
  int switcher = digitalRead(2);


   if(x_axis <= 400) //&& x < 7)
   {
     snake[0].following_direction = 0;
     //x += 1;
     //Serial.println("right");
   }


   else if(x_axis >= 600) //&& x > 0)
   {
     snake[0].following_direction = 1;
     //x -= 1;
     //Serial.println("left");
   }


   else if(y_axis <= 400) //&& y > 0)
   {
     snake[0].following_direction = 2;
     //y -= 1;
     //Serial.println("up");
   }


   else if(y_axis >= 600) //&& y < 7)
   {
     snake[0].following_direction = 3;
     //y += 1;
     //Serial.println("down");
   }





  if(snake[0].following_direction == 0 && snake[0].x < 7)
  {
    snake[0].previous_x = snake[0].x;
    snake[0].x += 1;
  }

  else if(snake[0].following_direction == 1 && snake[0].x > 0)
  {
    snake[0].previous_x = snake[0].x;
    snake[0].x -= 1;
  }

  else if(snake[0].following_direction == 2 && snake[0].y > 0)
  {
    snake[0].previous_y = snake[0].y;
    snake[0].y -= 1;
  }

  else if(snake[0].following_direction == 3 && snake[0].y < 7)
  {
    snake[0].previous_y = snake[0].y;
    snake[0].y += 1;
  }


  for(int a = 1; a < 64; a++)
  {
    if(snake[i] != 0)
    {

      snake[i].x = snake[i-1].previous_x;
      snake[i].y = snake[i-1].previous_y;    
    }
  }


  lc.setLed(0,power_up->x,power_up->y,true);
  for(int w = 0; w < 64; w++)
  {
    if(snake[w] != 0)
    {
      lc.setLed(0,snake[w].x,snake[w].y,true);  
    }
  }

  if(snake[0].x == power_up->x && snake[0].y == power_up->y)
  {
    power_up->x = random(0, 8);
    power_up->y = random(0, 8);


    for(int i = 0; i < 64; i++)
    {
      if(snake[i] == 0)
      {
         Snake_body *snake[i];

         //next_snake_body = new Snake_body();
         snake[i] = new Snake_body(snake[i-1].previous_x, snake[i-1].previous_y);
         //Snake_body next_snake_body;
         //snake[i].configure(snake[i-1].previous_x, snake[i-1].previous_y);
      }
    }
  }
  /*Serial.print("x: ");
  Serial.print(snake.x);
  Serial.println();

  Serial.print("y: ");
  Serial.print(snake.y);
  Serial.println();*/

  if(switcher == 0)
  {
    delay(50); 
  }
  else
  {
    delay(100); 
  }
  lc.clearDisplay(0);
}

the error is coming from this line here:
else if(snake[0].following_direction == 3 && snake[0].y < 7)

but I think it is also happening at all other lines of which are similar to this one.
I have tried replacing the . in snake[0].following_direction with a -> instead but that just caused the error: base operand of '-> ' is not a pointer. I have experience programming in java python and a few others but not arduino C++. 

user31105: The problem is indeed in that line:
else if(snake[0].following_direction == 3 && snake[0].y < 7)

And specifically in:
snake[0].following_direction

snake is defined as
int snake[64]; 

Thus snake[0] is of type int.
And you cannot request the member following_direction from type.
Since you make new instances with:
snake[0] = new Snake_body(0,0)

I assume snake should be an array of Snake_body.
So I suggest (at least) change the type to:
Snake_body snake[64]; 

Just as a side note, I think one instance of Snake_body can take up like 16 bytes, and you make 64 of them, this results in 1,024 bytes which is half the memory of an Arduino Uno. Also, sometimes a library takes up some space, so keep this in mind if you are intending to add more libraries/global variables or stack space.
Update
Also the following line seems wrong:
snake[0] = new Snake_body(0,0);

You already created 64 instances of Snake_body (and assigned to snake) after you do my change. So you do not need to do this yourself; actually you cannot, unless you create an array of POINTERS to Snake_body.

61967: <p>I am trying to create a basic snake game using the arduino UNO along with the MAX72XX module, the LCD module and the analog joystick module. I am wanting to program my program using object orientated programming. I have no experience with C++ so I am struggling quite a bit to understand what the following error is trying to say to me: <code>request for member 'following_direction' in 'snake[0]', which is of non-class type 'int'</code>.Here is the full code:</p>

<pre><code>#include "LedControl.h"
#include &lt;LiquidCrystal.h&gt;
LedControl lc=LedControl(12,10,11,1);
LiquidCrystal lcd(8, 13, 3, 5, 6, 7);


/*lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);*/

class Snake_body
{
  //private:




  public:

    int following_direction;
    int body_length[64];
    int x;
    int y;
    int previous_x;
    int previous_y;

    Snake_body(int x_value, int y_value) 
    {
      this-&gt;following_direction = 1;
      this-&gt;x = x_value;
      this-&gt;y = y_value;
      this-&gt;previous_y = y_value;
      this-&gt;previous_x = x_value;
    }
    /*void configure(int x_value, int y_value)
    {
      this-&gt;following_direction = 1;
      this-&gt;x = x_value;
      this-&gt;y = y_value;
      this-&gt;previous_y = y_value;
      this-&gt;previous_x = x_value;  
    }*/

};


class Power_up
{
  private:

  public:
    virtual void init() 
    {
      this-&gt;x = 0;
      this-&gt;y = 0;
    }

    int x;
    int y;
};

int snake[64];
int snakes_length;


Snake_body *snake_body;
Power_up *power_up;



void setup() {

  lcd.begin(16, 1);

  snakes_length = 1;

  for(int z = 1; z &lt; 64; z++)
  {
  snake[z] = 0;
  }

  snake[0] = new Snake_body(0,0);
  //Snake_body snake[0] = new Snake_body();
  //snake[0].configure(0,0);
  // put your setup code here, to run once:
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);

  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  Serial.begin(9600);
  power_up-&gt;x = random(0, 8);
  power_up-&gt;y = random(0, 8);
}

int snake_direction;


void loop() 
{
  int x_axis = analogRead(A0);
  int y_axis = analogRead(A1);
  int switcher = digitalRead(2);


   if(x_axis &lt;= 400) //&amp;&amp; x &lt; 7)
   {
     snake[0].following_direction = 0;
     //x += 1;
     //Serial.println("right");
   }


   else if(x_axis &gt;= 600) //&amp;&amp; x &gt; 0)
   {
     snake[0].following_direction = 1;
     //x -= 1;
     //Serial.println("left");
   }


   else if(y_axis &lt;= 400) //&amp;&amp; y &gt; 0)
   {
     snake[0].following_direction = 2;
     //y -= 1;
     //Serial.println("up");
   }


   else if(y_axis &gt;= 600) //&amp;&amp; y &lt; 7)
   {
     snake[0].following_direction = 3;
     //y += 1;
     //Serial.println("down");
   }





  if(snake[0].following_direction == 0 &amp;&amp; snake[0].x &lt; 7)
  {
    snake[0].previous_x = snake[0].x;
    snake[0].x += 1;
  }

  else if(snake[0].following_direction == 1 &amp;&amp; snake[0].x &gt; 0)
  {
    snake[0].previous_x = snake[0].x;
    snake[0].x -= 1;
  }

  else if(snake[0].following_direction == 2 &amp;&amp; snake[0].y &gt; 0)
  {
    snake[0].previous_y = snake[0].y;
    snake[0].y -= 1;
  }

  else if(snake[0].following_direction == 3 &amp;&amp; snake[0].y &lt; 7)
  {
    snake[0].previous_y = snake[0].y;
    snake[0].y += 1;
  }


  for(int a = 1; a &lt; 64; a++)
  {
    if(snake[i] != 0)
    {

      snake[i].x = snake[i-1].previous_x;
      snake[i].y = snake[i-1].previous_y;    
    }
  }


  lc.setLed(0,power_up-&gt;x,power_up-&gt;y,true);
  for(int w = 0; w &lt; 64; w++)
  {
    if(snake[w] != 0)
    {
      lc.setLed(0,snake[w].x,snake[w].y,true);  
    }
  }

  if(snake[0].x == power_up-&gt;x &amp;&amp; snake[0].y == power_up-&gt;y)
  {
    power_up-&gt;x = random(0, 8);
    power_up-&gt;y = random(0, 8);


    for(int i = 0; i &lt; 64; i++)
    {
      if(snake[i] == 0)
      {
         Snake_body *snake[i];

         //next_snake_body = new Snake_body();
         snake[i] = new Snake_body(snake[i-1].previous_x, snake[i-1].previous_y);
         //Snake_body next_snake_body;
         //snake[i].configure(snake[i-1].previous_x, snake[i-1].previous_y);
      }
    }
  }
  /*Serial.print("x: ");
  Serial.print(snake.x);
  Serial.println();

  Serial.print("y: ");
  Serial.print(snake.y);
  Serial.println();*/

  if(switcher == 0)
  {
    delay(50); 
  }
  else
  {
    delay(100); 
  }
  lc.clearDisplay(0);
}
</code></pre>

<p>the error is coming from this line here:</p>

<pre><code>else if(snake[0].following_direction == 3 &amp;&amp; snake[0].y &lt; 7)
</code></pre>

<p>but I think it is also happening at all other lines of which are similar to this one.</p>

<p>I have tried replacing the <code>.</code> in <code>snake[0].following_direction</code> with a <code>-&gt;</code> instead but that just caused the error: <code>base operand of '-&gt; ' is not a pointer</code>. I have experience programming in java python and a few others but not arduino C++. </p>
 31105: <p>The problem is indeed in that line:</p>

<pre><code>else if(snake[0].following_direction == 3 &amp;&amp; snake[0].y &lt; 7)
</code></pre>

<p>And specifically in:</p>

<pre><code>snake[0].following_direction
</code></pre>

<p>snake is defined as</p>

<pre><code>int snake[64]; 
</code></pre>

<p>Thus <code>snake[0]</code> is of type <code>int</code>.
And you cannot request the member <code>following_direction</code> from type.
Since you make new instances with:</p>

<pre><code>snake[0] = new Snake_body(0,0)
</code></pre>

<p>I assume <code>snake</code> should be an array of Snake_body.</p>

<p>So I suggest (at least) change the type to:</p>

<pre><code>Snake_body snake[64]; 
</code></pre>

<p>Just as a side note, I think one instance of Snake_body can take up like 16 bytes, and you make 64 of them, this results in 1,024 bytes which is half the memory of an Arduino Uno. Also, sometimes a library takes up some space, so keep this in mind if you are intending to add more libraries/global variables or stack space.</p>

<p><strong>Update</strong></p>

<p>Also the following line seems wrong:</p>

<pre><code>snake[0] = new Snake_body(0,0);
</code></pre>

<p>You already created 64 instances of Snake_body (and assigned to <code>snake</code>) after you do my change. So you do not need to do this yourself; actually you cannot, unless you create an array of POINTERS to <code>Snake_body</code>.</p>
