C++ Question

I can not have a Constructor method / prototype that takes multiple integers as arguments? Arduino?

I want to make a my Constructor that need two arguments then use it to make some instances, but I get errors like this.

*"Too_Many_Moles:8: error: conversion from 'mole*' to non-scalar type 'mole' requested
mole mole6 = new mole( 7, 13 );

^
exit status 1
conversion from 'mole*' to non-scalar type 'mole' requested"*


what I am useing


  • Arduino/Genuino Uno chip mega328u

  • Arduino IDE V1.6.12

  • this should not matter because I just need it to verify



main.ino file

#include <mole.h>

mole mole1 = new mole( 2, 8 );
mole mole2 = new mole( 3, 9 );
mole mole3 = new mole( 4, 10 );
mole mole4 = new mole( 5, 11 );
mole mole5 = new mole( 6, 12 );
mole mole6 = new mole( 7, 13 );

//I whould also like a array of moles here so I can do something like this.
//moleArray[0].sould_be_up = true;
void setup() {
// put your setup code here, to run once:
}

void loop() {
// put your main code here, to run repeatedly:
}


mole.h file

#ifndef mole_h
#define mole_h

#include "Arduino.h"

class mole
{
public:
mole(int p_out, int p_in);
void changeMoleSate();
void resetMole();

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

//I think I will move these to that main class
//************************
void decrementUpTimer();
//************************

void setRecoveryTimer(int recovery_timer);
int getRecoveryTimer();

//************************
void decrementRecoveryTimer();
//************************

boolean sould_be_up = false;

private:
int _up_timer = 0;
int _recovery_timer = 0;
int _pin_out;
int _pin_in;
// todo
//void setPinsModes();
};
#endif


mole.cpp file

#include "Arduino.h"
#include "mole.h"

mole::mole(int p_out, int p_in) {
this._pin_out = p_out;
this._pin_in = p_in;
}

// todo
/*void mole::setPinsModes() {
pinMode(_pino , OUTPUT);
pinMode(_pini , INPUT );
}*/

void mole::changeMoleSate() {
if (sould_be_up == false) {
digitalWrite(_pino, LOW);
}
else {
digitalWrite(_pini, HIGH);
}
}

void mole::resetMole() {
sould_be_up = false;
_up_timer = 0;
_recovery_timer = 0;
digitalWrite(_pino, LOW);
}


void mole::setUpTimer(int up_timer) {
_up_timer = up_timer;
}

int mole::getUpTimer() {
return _up_timer;
}

void mole::decrementUpTimer() {
if (_up_timer > 0) {
_up_timer--;
}
}


void mole::setRecoveryTimer(int recovery_timer) {
_recovery_timer += recovery_timer;
}

int mole::getRecoveryTimer() {
return _recovery_timer;
}

void mole::decrementRecoveryTimer() {
if (_recovery_timer > 0) {
_recovery_timer--;
}
}

Answer
mole mole1 = new mole( 2, 8  );

This attempts to create a mole object on the free store and save the pointer to that object in mole1. That won't work. Just create the mole1 object directly:

mole mole1( 2, 8  );
Comments