简体   繁体   English

如何在“ int main”中使用标题函数?

[英]How to use a header function in 'int main'?

I am working on a project that has a class (Time) defined in a header file and the objective is to use that class in my main function to determine the difference between two times. 我正在一个项目中,该项目在头文件中定义了一个类(时间),目的是在我的主函数中使用该类来确定两次之间的差。 I have gone over this is class and yet cannot wrap my head around the concept of using the class that has been defined for me and using it's accessor functions in main. 我已经遍历了这个class,但是无法理解使用为我定义的class以及在main中使用其访问器函数的概念。

I will post the header and what I have done so far and hopefully somebody can clarify what I need to do because I understand the objective and what I need to do to accomplish it but I just cannot seem to translate that into usable code... Hopefully someone can put this in terms comprehensible to an novice programmer like myself better than both my teacher and my text. 我将发布标头以及到目前为止我已完成的工作,希望有人可以阐明我需要做的事情,因为我了解目标以及实现目标所需要做的事情,但我似乎无法将其转换为可用的代码...希望有人可以像我这样的新手程序员用比我的老师和我的文字更好的方式来理解这个术语。

Header: 标头:

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

You're having some trouble with your object declarations for instances of Time, I gather. 我收集到,您在使用时间实例的对象声明时遇到了麻烦。 I'll admit that the C++ syntax fouls me up sometimes, but I think you need to replace the void Time(int hour, int min, int sec); 我会承认C ++语法有时void Time(int hour, int min, int sec);我感到困惑,但是我认为您需要替换void Time(int hour, int min, int sec); with something like Time startTime; Time startTime;这样的东西Time startTime; , and replace time_t now(); ,并替换time_t now(); with something like Time stopTime; Time stopTime; .

Then do int durationInSeconds = stopTime.seconds_from(startTime); 然后做int durationInSeconds = stopTime.seconds_from(startTime); and report durationInSeconds as the time spent typing. 并报告durationInSeconds作为键入所花费的时间。

Ok if I understand your question,what you should do is add an include at the top of your c class, so for this case you will add 好的,如果我理解您的问题,您应该做的是在c类的顶部添加一个include,因此在这种情况下,您将添加

#include "cccTime.h" 

(or whatever the file name is, use "" and not <> as you normally use for includes) (或任何文件名,使用“”而不是通常用于include的<>)

and then you can call the methods, as you always do 然后您可以像往常一样调用方法

You don't need to know what time it is. 您无需知道现在几点。 Just use the default constructor. 只需使用默认构造函数即可。 The default constructor " Constructs a Time object that is set to the time at which the constructor executes. " This makes it very easy to time how long something takes: 默认的构造函数“ 构造一个Time对象,该对象设置为构造函数执行的时间。 ”这使得计时很容易花费时间:

  Time start_time;
  do_lots_of_stuff ();
  Time end_time;

Now you can use end_time.seconds_from(start_time) to determine how much time elapsed from start to finish. 现在,您可以使用end_time.seconds_from(start_time)确定从开始到结束所花费的时间。

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

Am I right? 我对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM