c++ - 静态之前的预期主要表达?常量指针必须使用

标签 c++ static struct

由于作业的某些限制,我无法更改此代码中的很多内容。这是代码:

#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。

无论如何,有谁知道如何解决关于在静态之前需要主表达式的错误?

最佳答案

你想写的(我觉得)更像是

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

该行中 static 背后的意图是什么? 如果您多次调用此函数,那将导致问题,因为该行在程序的生命周期内只会执行一次(如果您放置 static),因此会给出错误的答案。

返回类型也关闭了。你需要修复 ->/. 正如@mux 解释的那样

编辑:您使用的是构造函数语法,但没有为 MyTime 定义构造函数,我认为您需要使用 MyTime var = {...} 语法而不是 MyTime var( ...)

关于c++ - 静态之前的预期主要表达?常量指针必须使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338976/

相关文章:

java - 卡夫卡 Protocol Buffer : C++ serialization to java

c++ - 结构体是如何存储在内存中的?

ios - 为什么我的变量的值在Swift中恢复?

java - 从类中获取静态字段的值

代码可以编译,但在与游戏板一起运行时出现段错误

c - 在节点中传递 ')' token 之前预期为 '*'

c++ - Linux 进程文件包含哪些内容?

c++ - 一个类中的pthread

Android 静态变量和共享首选项

c++ - 具有基于位置的枚举编译错误的结构