C++中的时间函数通过引用 [英] time function in c++ by reference

查看:27
本文介绍了C++中的时间函数通过引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个相应地重写时间结构值的函数(如果分钟是 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.

错误:

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

推荐答案

结构名称 '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屋!

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