Pages: [1] 2   Go Down
Author Topic: function not declared in scope  (Read 13228 times)
0 Members and 2 Guests are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the program (not finished as you can see):
Code:
// 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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

cpp file:
Code:
// 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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I've broke it even more down and still getting the error. Here's the code as it is now:
Code:
int redInterval   = 0;
long redStartTime   = 0;

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

void loop()
{
}

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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)
{
}
 
« Last Edit: December 16, 2009, 05:05:15 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 16, 2009, 05:22:20 am by mem » Logged

Pages: [1] 2   Go Up
Jump to: