Custom Type in Function - "was not declared in this scope"

I am using the elapsedMillis library for some timer stuff.

I have a global variable declared like this:

elapsedMillis tmr;

I can use it without any problems in the setup() or loop(), however I want to pass it to a function as a parameter:

void test(elapsedMillis x) {
    
}

Then I want to call it like this:

test(tmr);

However, when I compile, then I get: “error: variable or field ‘test’ declared void”.

Why cant I pass in a variable with type elapsedMillis? But int/bool/etc work?

If you want to pass objects you’d rather want to use references void test(myClass& myRef)

And if you only want to pass the millis count (doh’ :blush:) go with @peekay123’s suggestion (why did I not think of that interpretation of the question):

@Carsten4207, elapsedMillis is a class, not a type. The “type” is unsigned long I believe so you should declare void test(unsigned long x). :smile:

1 Like

This sounds like a preprocessor issue, please add

#pragma SPARK_NO_PREPROCESSOR

to the top of your .ino file.

So that will just pass in the current duration of the elapsedMillis correct? What if I want to reset it back to 0 from within the function?

I believe I would have to use a pointer correct?

This doesnt work. Get the same error.

@Carsten4207, assuming you declared tmr globally, then just set tmr = 0; anywhere!

Yeah I know I can do that, but was just curious if there is a better way of doing it.

My test code:

int x = 0;

void setup() {
   
    fun(x, 1);
    fun(x, 2);
     
}

void fun(int &variableName, int value) {
    variableName  = value;
}

With the code above, I can call the function (fun), pass in the global variable I want to update, and assign a value.

I was hoping I could do the same that I did with the int but with a ElapsedTimer.

Sorry if I wasn’t clear initially.

Stands to reason that if you call a subroutine that is set as void, you cant expect to receive a value back from it.therefore the preprocessor naturally will complain that the function was not declared.
In C++ a void function does not return a value so you can not call it to return a value.

As you stated above

I'd go with @peekay123's suggestion.
But as for my proposal, this builds for my

#pragma SPARK_NO_PREPROCESSOR
#include "application.h"

#include "elapsedMillis/elapsedMillis.h"

elapsedMillis tmr;
volatile int i;

void test(elapsedMillis& x)
{
    x += i++;
    return;
}

void setup() { }

void loop() 
{
    i++;
    test(tmr);
}
1 Like

Are you using the WebIDE? I get a bunch of errors with the code above.

Yes, I am - have you copy-pasted my code?
The error there looks like you have not added #include "application.h" which is required when using #pragma SPARK_NO_PREPORCESSOR

1 Like

Sorry I got it. I had #include “elapsedMillis/elapsedMillis.h” on the very top. Had to have it in this sequence:

#pragma SPARK_NO_PREPROCESSOR
#include "application.h"

#include "elapsedMillis/elapsedMillis.h"

Thank you! This will work!

3 Likes