微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++中的时间函数通过引用

如何解决C++中的时间函数通过引用

我正在研究一个相应地重写时间结构值的函数(如果分钟是 128,它将更改为 2 小时 8 分钟)

现在不要太在意实际功能,我的问题似乎出在声明/参数/结构本身。

错误

  1. 错误(活动)E0070 不允许不完整的类型
  2. 错误(活动)E0266“时间”不明确
  3. 错误(活动)E0065 应为“;”

我做错了什么?

谢谢。

#include<iostream>
#include<iomanip>
//#include<math.h>
//void canonify(time&);
using namespace std;
//
typedef struct time
{
    int h,min,sec;
};
//
const int full = 60;
//
void canonify(time& pre)  // eror1 and eror2
{                         // eror3
pre.min += pre.sec / full;
pre.h += pre.min/ full;
pre.sec %= full;
pre.min %= full;
}
void main()
{
    time a;
    a.h = 3;
    a.min = 128;
    a.sec = 70;
    canonify(a);
    cout << a.h <<":"<< a.min <<":"<< a.sec << endl;
}

解决方法

结构名称“时间”导致冲突。您可以将 struct 的名称更改为其他名称并将 typedef struct 替换为 struct ,否则编译器本身将忽略它。

此代码对我有用。

#include <iostream>
#include <iomanip>
//#include<math.h>
//void canonify(ti&);
using namespace std;
//
struct ti                               //change
{
    int h,min,sec;
};
//
const int full = 60;
//
void canonify(ti &pre)                 //change
{
    int hour;
    int minute;
    int second;
    minute = pre.sec / full;
    second = minute * full - pre.sec;
    hour = pre.min / full;
    minute = hour * full - pre.min;
    pre.h = hour;
    pre.min = minute;
    pre.sec = second;
}
int main()
{
    ti a;                              //change
    a.h = 3;
    a.min = 128;
    a.sec = 70;
    canonify(a);
    cout << a.h << ":" << a.min << ":" << a.sec << endl;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。