C++ Question

invalid use of void expression, what how?

Don't know why I'm getting this.

The program is a badly coded whack a mole game made to run on a Arduino Uno.

My IED is Arduino IED version 1.6.12

Yes, there's a lot more to it but chose to leave it out, if you want to see all the code just ask.

Error

Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Strings\Documents\Arduino\Too_Many_Moles\Too_Many_Moles.ino: In function 'void winOrLost()':

Too_Many_Moles:146: error: invalid use of void expression

timelyCall(current_millis, toggleBuzer(), 1000);

^

C:\Users\Strings\Documents\Arduino\Too_Many_Moles\Too_Many_Moles.ino: In function 'void loop()':

Too_Many_Moles:191: error: invalid use of void expression

timelyCall(current_millis, popAMole(), time_interval);

^

exit status 1
invalid use of void expression

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Main.ino

const int buzer = A0, on_off_pin = A1;

unsigned long current_millis;
const int time_interval = 20;
int currentMole = 0;

int points = 0, lives = 3, level = 0, odds_of_pop_up = 6;

mole moles[6] = {
mole( 2, 8 ),
mole( 3, 9 ),
mole( 4, 10 ),
mole( 5, 11 ),
mole( 6, 12 ),
mole( 7, 13 )
};
int number_of_mole = sizeof(moles) / sizeof(*moles);

void timelyCall(unsigned long currentMillis, void (&f)(), int ms) {
if (millis() - currentMillis > ms) {
f();
currentMillis = millis();
}
}
void popAMole() {
if (moles[currentMole].isReadyToPop()) {
moles[currentMole].popUp(odds_of_pop_up);
}
if (currentMole + 1 <= number_of_mole) {
currentMole = 0;
}
else {
currentMole++;
}
}


mole.h

#ifndef mole_h
#define mole_h

#include "Arduino.h"

class mole
{
public:
mole(int p_out, int p_in);
void resetMole();
void popUp(int odds);
boolean moleGetHit();
boolean isReadyToPop();

void setUpTimer(int up_timer);
int getUpTimer();
void decrementUpTimer();

void setRecoveryTimer(int recovery_timer);
int getRecoveryTimer();
void decrementRecoveryTimer();
void addRecoveryTimer(int add_time);

boolean getSouldBeUp();

int getOddsOfPopinh();
void setOddsOfPopinh(int odds);

int getPinIn();


private:
boolean _sould_be_up = false;
int _up_timer = 0;
int _recovery_timer = 0;
int _pin_out;
int _pin_in;
int _odds_of_poping = 10;

};
#endif


mole.cpp

boolean mole::isReadyToPop() {
if (_recovery_timer == 0 && _sould_be_up = false) {
return true;
}
else {
return false;
}
}

void mole::popUp(int odds) {
if (rand() % odds + 1 == 1) {
digitalWrite(_pin_out, HIGH)
_sould_be_up = true;
}
}

Answer

You are calling the functions not passing the addresses to timelyCall!

timelyCall(current_millis, popAMole(), time_interval);
timelyCall(current_millis, toggleBuzer(), 1000);

Should be:

timelyCall(current_millis, popAMole, time_interval);
timelyCall(current_millis, toggleBuzer, 1000);

Notice no brackets on popAMole & toggleBuzer.