Instantiate class in a library by method in the Norwegian

Asked

Viewed 194 times

1

I have this line of code, and in it I have 2 methods, where one sets up the keypad and another takes the key pressed.

void Maleta::setKeypad(int r1, int r2, int r3, int r4, int c1, int c2, int c3, int c4){
  const byte numRows= 4; // Numero de linhas
  const byte numCols= 4; // Numero de colunas

  char keymap[numRows][numCols]=
  {
   {'1','2','3','A'},
   {'4','5','6','B'},
   {'7','8','9','C'},
   {'*','0','#','D'},
  };

  byte rowPins[numRows] = {r1,r2,r3,r4}; // Pinos digitais onde as linhas estao conectadas
  byte colPins[numCols] = {c1,c2,c3,c4}; // Pinos digitais onde as colunas estao conectadas


  Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
}
char Maleta::getKeyPress(){
  char keypressed = myKeypad.getKey();
  return keypressed;
}

This is the error that returns:

C: Users Luca Documents Arduino Briefcase Briefcase Briefcase.cpp: In Member Function 'char Briefcase::getKeyPress()':

C: Users Luca Documents Arduino Briefcase Briefcase.cpp:32:21: error: 'myKeypad' was not declared in this Scope
char keypressed = myKeypad.getKey();

2 answers

0

The problem is occurring because the variable myKeypad cannot be accessed within getKeyPress, since it is not visible in that scope.

Note that the variable is declared in another method, called setKeypad. The scope of the methods getKeyPress and setKeypad are distinct. If myKeypad is a global variable, so yes it could be accessed, because it would be visible in all the scope of the program. However, global variables, as a general rule, should be avoided.

0

char Maleta::getKeyPress(){
  //Exemplo Maleta2 mykeypad; voce teria declarada o mykeypad.
  char keypressed = myKeypad.getKey();// assim você chama mykeypad depois o membro .getKey()
  return keypressed;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.