Frage

I was hoping some of you code geniuses might help a coding impaired individual like myself. I have to create this program that puts a timestamp on another program I previously created. Right now Im in the process of trying to obtain time using the gettimeofday function in C++ (We are doing this in Unix btw).

Anyways I have a small section of code ready to compile except i keep getting 2 particular errors. Perhaps if someone could help me in that regard and also give me some advice on how the code looks so far that'd be great...

#include <curses.h>
#include <sys/time.h>
#include <time.h>

ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime);

struct ExpandedTime
{
    double et_usec;
    double et_sec;
    double et_min;
    double et_hour;

};

int main()
{
    struct timeval tv;
    struct ExpandedTime etime;
    gettimeofday(&tv, NULL);
    localTime(tv, ExpandedTime);

}

ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime)
{

}

Basically right now im just trying to properly use gettimeofday as well as pass the timeval structure defined as tv and the expanded time structure into the actual localtime function....however line 33, where I call the localtime function give me 2 particular errors.

  1. localtime was not declared in this scope
  2. expected primary expression before ')' token

Any help would be much appreciated......The expandedtime function is suppose to receive the values of gettimeofday which is stored in some structure in one of the included header files i believe.

War es hilfreich?

Lösung

ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime);

At this point, the compiler has no idea what ExpandedTime is. You have to move this to after the declaration.

Also you have:

localTime(tv, ExpandedTime);

That should be:

localTime(tv, &etime);

Andere Tipps

I'd recommend using typedef with your structs to simplify calling them. (I honestly couldn't get the above to compile.)

Normally, you would need to use "strut ExpandedTime" everywhere, I would think.

The only way I know how to just use "ExpandedType" alone as a struct is to typedef it, as in:

typedef struct expanded_time_struct {
// your struct's data
} ExpandedTime;

So in your case, something like:

typedef struct ExpandedTime_struct
{
    double et_usec;
    double et_sec;
    double et_min;
    double et_hour;

} ExpandedTime;

ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime);
int main()
{
    struct timeval tv;
    ExpandedTime etime;
    gettimeofday(&tv, NULL);
    localTime(&tv, &etime);

}

ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime)
{

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top