【问题标题】:How to use a header function in 'int main'?如何在“int main”中使用标题函数?
【发布时间】:2011-09-25 15:19:23
【问题描述】:

我正在开发一个项目,该项目在头文件中定义了一个类(时间),目标是在我的主函数中使用该类来确定两次之间的差异。我已经讨论了这个类,但我无法理解使用为我定义的类并在 main.js 中使用它的访问器函数的概念。

我将发布标题以及到目前为止我所做的事情,希望有人能澄清我需要做什么,因为我了解目标以及完成它需要做什么,但我似乎无法将其转化为可用的代码...希望有人能用比我的老师和我的课文更能理解像我这样的新手程序员的术语来表达这一点。

标题:

#ifndef CCC_TIME_H
#define CCC_TIME_H

/**
   A class that describes a time of day
   (between 00:00:00 and 23:59:59)
*/
class Time
{
public:
   /**
      Constructs a time of day.
      @param hour the hours
      @param min the minutes
      @param sec the seconds
    */
   Time(int hour, int min, int sec);
   /**
  Constructs a Time object that is set to 
  the time at which the constructor executes.
*/
Time();

/**
  Gets the hours of this time.
  @return the hours
*/
int get_hours() const;
/**
  Gets the minutes of this time.
  @return the minutes
*/
int get_minutes() const;
/**
  Gets the seconds of this time.
  @return the seconds
*/
int get_seconds() const;

/**
  Computes the seconds between this time and another.
  @param t the other time
  @return the number of seconds between this time and t
*/
int seconds_from(Time t) const;
/**
  Adds a number of seconds to this time.
  @param s the number of seconds to add
*/
void add_seconds(int s);

private:
int time_in_secs;
};

#endif

___________________


using namespace std;

int main()
{
int t;
string x;

cout<< "This program will test your typing speed."<< endl;      
cout<< "Type the following:"<<endl;
cout<<" "<<endl;
cout<< "The quick brown fox jumped over the lazy dog"<< endl; 
Time startTime;
getline(cin, x, '\n');

if(x == "The quick brown fox jumped over the lazy dog")
{
     Time endTime;
     int durationInSeconds = endTime.seconds_from(startTime);
     t = durationInSeconds;

}

     else{cout<<"Invalid text entered";}
 cout<<"You finished in: " << t << "seconds." endl;

system ("pause");
return 0;
}    

【问题讨论】:

  • 指定这是什么语言可能会有所帮助。可以猜测它是 C++,但也可能是其他几种。
  • 我真的很感激任何帮助,我不希望任何人为我写这个我真的可以使用一些帮助(例如,我做错了什么,我必须创建开始和结束时间的变量?等)
  • 你应该在你的问题中添加一个作业标签。
  • 这应该是做什么的? void Time(int hour, int min, int sec); 这似乎是 Time(...) 构造函数的声明,您已经从 H 文件中获得了该构造函数。我怀疑你需要构造一个时间变量。
  • 是的,我认为同样的事情,我最初有 Time() 但我不知道我是否应该有参数或者它们是隐含的?

标签: c++ class function member


【解决方案1】:

我猜你在声明 Time 实例时遇到了一些问题。我承认 C++ 语法有时会让我感到困惑,但我认为您需要将 void Time(int hour, int min, int sec); 替换为 Time startTime; 之类的东西,并将 time_t now(); 替换为 Time stopTime; 之类的东西。

然后执行int durationInSeconds = stopTime.seconds_from(startTime); 并报告durationInSeconds 作为输入时间。

【讨论】:

  • 我已经修改了代码并且已经看到我有什么错误,但是我现在对我的返回值感到困惑。我应该把我的cout放在哪里?主要吗?
  • 没关系,我觉得我有点依赖了...我声明了一个 int 变量,然后将 durationInSeconds 的值分配给它,并在我的 cout 语句中使用了该变量。跨度>
【解决方案2】:

好的,如果我理解你的问题,你应该做的是在你的 c 类的顶部添加一个包含,所以对于这种情况你将添加

#include "cccTime.h" 

(或者不管文件名是什么,使用 "" 而不是 就像你通常用于包含的那样)

然后你可以像往常一样调用方法

【讨论】:

  • 哦抱歉没看到,我现在知道这是基本的练习了,已经有人在帮助他了
【解决方案3】:

您无需知道现在几点。只需使用默认构造函数。默认构造函数“构造一个 Time 对象,该对象设置为构造函数执行的时间。”这使得计算某件事需要多长时间变得非常容易:

  Time start_time;
  do_lots_of_stuff ();
  Time end_time;

现在您可以使用end_time.seconds_from(start_time) 来确定从开始到结束所用的时间。

【讨论】:

    【解决方案4】:
    #include<iostream>
    #include<string>
    
    #include "ccc_time.h"
    
    using namespace std;
    
    int main()
    {
      //Time start_time;                                                                                                                                                                                               
      string x;
    
      const string SAMPLE = "The quick brown fox jumped over the lazy dog";
    
      cout<< "This program will test your typing speed."<< endl;
      cout<< "Type the following:"<<endl;
      cout<<" "<<endl;
      cout<< SAMPLE << endl;
    
      getline(cin, x, '\n');
    
      //Time end_time;                                                                                                                                                                                                 
    
      if(x.compare(SAMPLE) == 0)
        {
          //int res = 0;                                                                                                                                                                                               
          //res = end_time.seconds_from(start_time);                                                                                                                                                                   
          //cout << res << endl;                                                                                                                                                                                       
          //return res;                                                                                                                                                                                                
        } else {
        cout << "Invalid text entered" << endl;
      }
    
      return 0;
    }
    

    我说的对吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      相关资源
      最近更新 更多