【问题标题】:Values not being received by functions函数未接收到值
【发布时间】:2014-05-19 02:35:03
【问题描述】:

所以我正在尝试制作一个程序,该程序需要随机输入的秒数并将其转换为天、小时、分钟和秒。我必须使用符号常量来定义一天中的小时数、一小时内的分钟数和一分钟内的秒数。我通过了这个值,但它没有被接收,所以我最终得到了一些巨大的负数。这是代码。如果有人能告诉我问题出在哪里,我将不胜感激。

我使用函数定义代码中的随机代码位来计算总秒数,以查看它是否被接收。

#ifndef SECONDS_H_
#define SECONDS_H_

#define HOURS_IN_DAY 24
#define MINUTES_IN_HOUR 60
#define SECONDS_IN_MINUTES 60

#include <iostream>

using namespace std;

class Seconds
{
private:
    long totalSeconds;
public:
    Seconds();
    ~Seconds(){};
    Seconds(int totalSeconds);
    void Seconds::convertSeconds(int &days, int &hours, int &minutes, int &seconds);
};

#endif

...

#include <conio.h>
#include <string>
#include <iostream>
#include "seconds.h"

#define HOURS_IN_DAY 24
#define MINUTES_IN_HOUR 60
#define SECONDS_IN_MINUTE 60

Seconds::Seconds(int totalSeconds)
{
    totalSeconds = totalSeconds;
}

void Seconds::convertSeconds(int &days, int &hours, int &minutes, int &seconds)
{   
    cout << endl;
    cout << "Total Seconds: " << totalSeconds;
    cout << endl;

    days = totalSeconds / MINUTES_IN_HOUR / SECONDS_IN_MINUTE / HOURS_IN_DAY;
    hours = (totalSeconds / MINUTES_IN_HOUR / SECONDS_IN_MINUTE) % HOURS_IN_DAY;
    minutes = (totalSeconds / MINUTES_IN_HOUR) % SECONDS_IN_MINUTE;
    seconds = (totalSeconds % SECONDS_IN_MINUTE);

}

...

#include <iostream>
#include <conio.h>
#include <string>
#include "seconds.h"

#define HOURS_IN_DAY 24
#define MINUTES_IN_HOUR 60
#define SECONDS_IN_MINUTES 60

using namespace std;

int main ()
{
    int totalSeconds;
    int days = 0, hours = 0, minutes = 0, seconds = 0;

    cout << "Enter a random massive amount of seconds: ";
    cin >> totalSeconds;

    Seconds sec(totalSeconds);

    sec.convertSeconds(days, hours, minutes, seconds);

    cout << "That is equivalent to " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds." << endl;

    cout << "Press any key to continue...";
    cin.sync();
    _getch();

    return 0;
}

【问题讨论】:

  • 您提供的“秒”值是多少?您是否尝试将其打印出来或在调试器中显示?
  • 另外,当你声明类时,你不应该在方法声明中使用类名:void Seconds::convertSeconds。一些编译器会对此发出警告,而另一些编译器则不会根据编译器设置来编译你的东西。这是一个品味问题,但如果你有很多 func 参数,那么我认为最好将这些值打包到一个结构中并将结构引用或结构 const 引用传递给函数。以后添加更多参数更容易。虽然执行函数调用需要更多的行,但代码变得更具可读性。

标签: c++ function


【解决方案1】:

这是个问题:

Seconds::Seconds(int totalSeconds)
{
    totalSeconds = totalSeconds;
}

函数参数totalSeconds遮蔽了类成员,所以这段代码就像在做x = x;一样,对this-&gt;totalSeconds没有影响。

要解决这个问题,要么使用不同的变量名,要么最好使用构造函数初始化语法:

Seconds::Seconds(long totalSeconds)
   : totalSeconds(totalSeconds)
{
}

在此版本中,不会发生阴影,因为构造函数初始化列表很智能。

【讨论】:

  • 在初始化列表的情况下不会出现阴影,因为totalSeconds 参数不能合法地出现在该上下文中,而不是因为初始化列表是智能的。是同一个编译器。
  • @Matt McNabb - 谢谢谢谢谢谢谢谢谢谢
  • 最好使用初始化器一个不同的名字。我知道 C++ 程序员应该知道使用相同的名称将与初始化程序一起使用,但是为什么还要让某人考虑一下呢?只需在参数名称后加上一个下划线(如果成员变量的命名约定使用尾随下划线,则不要使用它)。
【解决方案2】:

你有没有想过问题可能出在Integer Overflow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多