Expected primary expression before static? Constan

2019-08-28 01:38发布

问题:

I can't change a lot of things in this code due to certain limitations on the assignment. Here is the code:

#include <iostream>
#include<cstdlib>
#include<cstring>

using namespace std;
struct MyTime { int hours, minutes, seconds; };
int DetermineElapsedTime(const MyTime *t1, const MyTime *t2);
const int hourSeconds = 3600;
const int minSeconds = 60;

int DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{//problem about static in next line
    static MyTime ((long) (((t2.hours * hourSeconds) + (t2.minutes * minSeconds) +  t2.seconds) -
                   ((t1.hours * hourSeconds) + (t1.minutes * minSeconds) + t1.seconds)));
    return(MyTime);
}

It is not the entire thing but I need to somehow calculate the amount of time from primary input to the other. I will need to used setfill too.

Anyway, does anyone know how to fix the error about needing a primary expression before static?

回答1:

There are many problems in this piece of code.

First, you cannot return a struct type, you need to return a variable.

When creating your static variable, you failed to give it a name and use the = sign.

When using pointers you need to use -> instead of .

Now, the logic in your code will give you the difference in seconds, you will then need to convert it to proper hours, minutes and seconds to fill a MyTime variable. Here's an example of how you could compute the time (not tested, just an example):

int difference = (t2->hours * hoursSeconds + t2->minutes * minSeconds + t2->seconds) - (t1->hours * hoursSeconds + t1->minutes * minSeconds + t1->seconds); // this gives you a difference in seconds
int hoursDifference = difference / hoursSeconds; // how many full hours we have
difference -= hoursDifference * hoursSeconds; // remove from total what we just computed
int minsDifference = difference / minsSeconds; // how many full minutes we have
difference -= minsDifference * minsSeconds;
MyTime diff;
diff.hours = hoursDifference;
diff.minutes = minsDifference;
diff.seconds = difference;
return diff;


回答2:

what you want to write (i think) is more like

MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    MyTime var = { ...... };
    return var;
}

What was the intention behind the static in that line? That will cause issues if you call this function more than once, since that line will be executed only once during the life time of the program (if you put static), thus gonna give a wrong answer.

Also the return type was off. And you need fix the ->/. as @mux explains

edit: you were using a constuctor syntax, but no constructor is defined for MyTime, I think you need to use MyTime var = {...} syntax instead of MyTime var(...)



回答3:

you forgot the type and name of the variable, it seems like you want the difference between two MyTime objects, the difference seems to be returned as an int not in a new MyTime:

int t = ((int) (((t2->hours * hourSeconds) + (t2->minutes * minSeconds) +  t2.seconds) - ((t1->hours * hourSeconds) + (t1->minutes * minSeconds) + t1->seconds)));
return(t);

Also, t1 and t2 are pointers so use -> instead of . to access the members, and if you use static the variable will be initialized once and the same value is returned every time.