Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am getting a "does not name a type" error when trying to use makeTime() from the Time.h library, code snippet as follows:

#include<Time.h>

tmElements_t tmet;
tmet.Year = 2013-1970;
tmet.Month = 8;
tmet.Day = 28;
tmet.Hour = 18;
tmet.Minute = 30;
tmet.Second = 0;

time_t dateMet = makeTime(tmet);

Alternatively I've also tried time_t dateMet = maketime(&tmet); with the same result. I am no expert so go easy on me but am I missing something small here?

Thanks

share|improve this question
up vote 4 down vote accepted

You're trying to access the 'properties' (i.e. the members) of the tmet variable in a scope where they aren't accessible (outside of a code block). In this global scope of the program the compiler expects a type name on the left side of a statement (i.e. a variable or type declaration), but tmet's members like tmet.Year aren't types in and of themselves.

There are two solutions. You can move the access to the members of tmet into a code block like the setup function:

#include <Time.h>

tmElements_t tmet;
time_t dateMet;

void setup() {
  // put your setup code here, to run once:
    tmet.Year = 2013-1970;
    tmet.Month = 8;
    tmet.Day = 28;
    tmet.Hour = 18;
    tmet.Minute = 30;
    tmet.Second = 0;

    dateMet = makeTime(tmet);
}

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

}

Or you could declare and define tmet in one statement, which has a different syntax (since you have to provide the content of a tmElements_t in a single line:

#include <Time.h>

tmElements_t tmet2 = { 2013-1970, 8, 28, 18, 30, 0 };

time_t dateMet2 = makeTime(tmet2);

void setup() {
  // put your setup code here, to run once:
}

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

}

Both versions work right away from within the Arduino IDE (i.e. I tested them).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.