Need assistance building library

When I compile the Arduino code below I get the message "error: expected initializer before 'int'
int UpdateIntAverage10SequentialDatum::updateIntAverage10SequentialDatum(int datum"

The .cpp file is copied from the function in the Arduino code, which runs as expected. The error occurs when I replace the call to the update10SequentialDatum(int datum, int history[]) function with the call to the updateIntAverage10SequentialDatum() function from the library.

Arduino code is:

#include <UpdateIntAverage10SequentialDatum.h>

UpdateIntAverage10SequentialDatum updateIntAverage10SequentialDatum();
/*
   Arrays are passsed by reference!
*/
int ref[10] ;
int rate = 250;
unsigned long timer;
int sampleDatum;
boolean flag;

int update10SequentialDatum(int datum, int history[]) {
  /*
     updates history with new datum and calculates average int value of data.
  */
  int avg;
  int sum;

  sum = 0;
  // shift register values
  for (int i = 0 ; i < 9 ; i++) {
    history[i] = history[i + 1];
    sum = sum + history[i];
  }

  history[9] = datum;  // add latest voltage reading
  sum = sum + datum;
  avg = sum / 10;  //compute average value

  return avg;
}

void setup() {
  Serial.begin(9600);
  timer = millis();
  flag = true;
  sampleDatum = 0;
}

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



  if ((millis() - timer) >= rate) {

//    Serial.println(updateIntAverage10SequentialDatum(sampleDatum, ref));

//    for (int i = 0 ; i < 10 ; i++) {
//      Serial.print(ref[i]);
//      Serial.print(" ");
//    }
//    Serial.println("");
    timer = millis();
    
    if (flag) {
      sampleDatum = sampleDatum + 1;
    }
    else if (!flag) {
      sampleDatum = sampleDatum - 1;
    }

    if (sampleDatum == 13) {
      flag = false ;
    }
    else if (sampleDatum == -2) {
      flag = true ;
    }
  }
}

The .h file in the library is:

/*
  UpdateIntAverage10SequentialDatum.cpp - Library for updating rolling average of
  exactly 10 integers in an array.
  Created by Fred Perkins, March 10, 2021.
  Released into the public domain.
*/
#ifndef UpdateIntAverage10SequentialDatum_h
#define UpdateIntAverage10SequentialDatum_h

#include "Arduino.h"

class UpdateIntAverage10SequentialDatum
{
  public:
    UpdateIntAverage10SequentialDatum();  
    int updateIntAverage10SequentialDatum(int datum, int history[]);
  private:
    
};

#endif

The .cpp file in the library is:

/*
  UpdateIntAverage10SequentialDatum.cpp - Library for updating rolling average of
  exactly 10 integers in an array.
  Created by Fred Perkins, March 10, 2021.
  Released into the public domain.
*/

#include "Arduino.h"
#include "UpdateIntAverage10SequentialDatum.h"

/* 
   need terminal semicolon below but not in Morse!
*/

UpdateIntAverage10SequentialDatum::UpdateIntAverage10SequentialDatum()


int UpdateIntAverage10SequentialDatum::updateIntAverage10SequentialDatum(int datum, 
	int history[]) {
  /*
     updates history with new datum and calculates average int value of data.
     arrays are passed by reference, so updates are passed back to calling code.
  */
  int avg;
  int sum;

  sum = 0;
  // shift register values
  for (int i = 0 ; i < 9 ; i++) {
    history[i] = history[i + 1];
    sum = sum + history[i];
  }

  history[9] = datum;  // add latest voltage reading
  sum = sum + datum;
  avg = sum / 10;  //compute average value

  return avg;
}

Help would be appreciated. Thanks.

It's probably a bad idea to give the same name to both the Class and a member function of the Class.

The constructor in the cpp file is a function. It needs the typical brackets {}, even if it is empty.

And, leave off the '()' in the .ino file:

UpdateIntAverage10SequentialDatum updateIntAverage10SequentialDatum();

Also, that instance name is only different from the class name by one letter being capitalized. That's kind of confusing.

And, I don't see where you're calling the instance function in the .ino function.

Instead of passing in the array of data. Just have an addData(value) function/method. Allowing you to keep the array internal. Then, maybe later in life, you can update the class to use a dynamic array that can store N data points.

Or not..

-jim lee

Thanks. I'm a retired rocket scientist but new to programming now that I finally have time. May not be the absolute best retirement hobby.

Jim, thanks. I'll try that.

Changed the .ino line to "UpdateIntAverage10SequentialDatum smiley;" and the call to "Serial.println(smiley.updateIntAverage10SequentialDatum(sampleDatum, ref));"
after building the .cpp constructor with the (empty) brackets.

Works now. Thanks very much.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.