Go Down

Topic: function not declared in scope (Read 17105 times) previous topic - next topic

SeeDoubleYou

This is the program (not finished as you can see):
Code: [Select]

// CONSTANTS
const char STATE_DEFAULT  = 'A';

// ENVIRONMENT VARIABLES
long currentTime = 0;
char state = STATE_DEFAULT;
int redInterval   = 0;
int blueInterval  = 0;
int greenInterval = 0;
long redStartTime   = 0;
long blueStartTime  = 0;
long greenStartTime = 0;

void setup()
{
 Serial.begin(9600);
 setColorInterval(redStartTime, redInterval);
 setColorInterval(blueStartTime, blueInterval);
 ssetColorInterval(greenStartTime, greenInterval);
}

void loop()
{
}

void setColorInterval(long &startTime, int &interval, int minInterval, int maxInterval)
{
 if((startTime == 0) || (currentTime - startTime >= interval))
 {
   interval = random(minInterval, maxInterval);
   startTime = currentTime;
 }
}

void setColorInterval(long &startTime, int &interval)
{
 setColorInterval(startTime, interval, 0, 10000);
}


When I compile this code I get a "In function 'void setup()': error: 'setColorInterval' was not declared in this scope".
Even when I move the function call from setup to loop() it still gives me the error for loop. Why am I getting this error?

PaulS

setColorInterval is defined AFTER the call to it, in either setup or loop.

Move the function up, so that it is before setup and/or loop, and the problem will go away.

SeeDoubleYou

This works indead, but it should work without. I have written programs in arduino before in the same way and it worked perfect. This is because the arduino build environment automatically creates prototypes for functions in the main tab of a sketch so there is no need to put a function declaration before it is called. So my question remaines.

PaulS

Good question. It would be interesting to see the .cpp file that the IDE produced (in the applet directory).

SeeDoubleYou

cpp file:
Code: [Select]

// CONSTANTS
#include "WProgram.h"
void setup();
void loop();

const char STATE_DEFAULT  = 'A';

// ENVIRONMENT VARIABLES
long currentTime = 0;
char state = STATE_DEFAULT;
int redInterval   = 0;
int blueInterval  = 0;
int greenInterval = 0;
long redStartTime   = 0;
long blueStartTime  = 0;
long greenStartTime = 0;

void setup()
{
 Serial.begin(9600);
 setColorInterval(redStartTime, redInterval);
 setColorInterval(blueStartTime, blueInterval);
 ssetColorInterval(greenStartTime, greenInterval);
}

void loop()
{
}

void setColorInterval(long &startTime, int &interval, int minInterval, int maxInterval)
{
 if((startTime == 0) || (currentTime - startTime >= interval))
 {
   interval = random(minInterval, maxInterval);
   startTime = currentTime;
 }
}

void setColorInterval(long &startTime, int &interval)
{
 setColorInterval(startTime, interval, 0, 10000);
}

int main(void)
{
     init();

     setup();
   
     for (;;)
           loop();
       
     return 0;
}




It seems that something goes wrong since no prototype is created for the functions

PaulS

Quote
Code: [Select]
 setColorInterval(blueStartTime, blueInterval);
 ssetColorInterval(greenStartTime, greenInterval);
}


set and sset? Does the compiler complain about both, or just the sset version?

Mike Murdock

CW,

I notice that you've defined setColorInterval() twice, with different arguments.  I didn't think you could do this with 'C'-style functions.  I thought it could only be done inside a C++ class.

It's possible that this double definition is confusing the preprocessor, so it doesn't output a declaration for that function.  You might try changing it to one function to see if it fixes the problem.

Regards,

-Mike

SeeDoubleYou

I thought of that as well, but when i remove the second function (it can be done without of course) the problem remaines.

PaulS

@seeDoubleYou
If you are still having problems, post your code, again. After all the modifications, it's hard to tell what the problem is, since we can't see over your shoulder.

SeeDoubleYou

Ok, I've broke it even more down and still getting the error. Here's the code as it is now:
Code: [Select]

int redInterval   = 0;
long redStartTime   = 0;

void setup()
{
 Serial.begin(9600);
 setColorInterval(redStartTime, redInterval);
}

void loop()
{
}

void setColorInterval(long &startTime, int &interval)
{
}

SeeDoubleYou

It seems to go wrong with the references. When I remove them (which I don't want), compiling works.

SeeDoubleYou

@mem: I need the reference because I want to have a general function to set global variables.

mem

#12
Dec 16, 2009, 11:03 am Last Edit: Dec 16, 2009, 11:05 am by mem Reason: 1
I was in the process of updating my post, I don't think arduino creates prototypes for references

You could try adding an explicit prototype a the beginning of the sketch

Code: [Select]
int redInterval   = 0;
long redStartTime   = 0;

void setColorInterval(long &startTime, int &interval);

void setup()
{
 Serial.begin(9600);
 setColorInterval(redStartTime, redInterval);
}

void loop()
{
}

void setColorInterval(long &startTime, int &interval)
{
}

SeeDoubleYou

This seems to work indeed. Why isn't arduino creating prototypes for references?

mem

#14
Dec 16, 2009, 11:20 am Last Edit: Dec 16, 2009, 11:22 am by mem Reason: 1
I don't know, but FYI it doesn't create prototypes for user defined types either.

I guess that when the auto-prototype code was written, it was assumed that this functionality would not be needed.

Go Up