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

编程入门 行业动态 更新时间:2024-10-08 18:33:28
本文介绍了C++中的时间函数通过引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I was working on a function that rewrites the values of a time struct accordingly (if minutes are 128 it will change to 2 hours and 8 minutes)

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

don't mind too much the actual function right now, my problem seems to be with the declaration/parameters/struct itself.

错误:

错误(活动)E0070 不允许不完整的类型错误(活动)E0266时间";模棱两可错误(活动)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;
}

推荐答案

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

Struct name 'time' is causing the conflict. You can change name of the struct to something else and replace typedef struct to struct only otherwise it will be ignored by compiler itself.

此代码对我有用.

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

这篇关于C++中的时间函数通过引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 15:21:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157083.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!