静态之前的预期主要表达式?常量指针必须使用 [英] Expected primary expression before static? Constant pointer must use

查看:24
本文介绍了静态之前的预期主要表达式?常量指针必须使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于分配的某些限制,我无法更改此代码中的很多内容.代码如下:

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);
}

这不是全部,但我需要以某种方式计算从主要输入到另一个输入的时间.我也需要使用 setfill.

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?

推荐答案

这段代码有很多问题.

首先,你不能返回struct类型,你需要返回一个变量.

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 .

现在,代码中的逻辑将为您提供以秒为单位的差异,然后您需要将其转换为正确的小时、分钟和秒以填充 MyTime 变量.以下是如何计算时间的示例(未经测试,只是示例):

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;

这篇关于静态之前的预期主要表达式?常量指针必须使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆