C++ 如何在';int main';?

C++ 如何在';int main';?,c++,class,function,member,C++,Class,Function,Member,我正在处理一个在头文件中定义了类(Time)的项目,目标是在我的主函数中使用该类来确定两个时间之间的差异。我已经讨论过这个类,但是我还不能完全理解使用为我定义的类以及在main中使用它的访问器函数的概念 我将发布标题和我到目前为止所做的事情,希望有人能澄清我需要做什么,因为我了解目标以及我需要做什么来完成它,但我似乎无法将其转化为可用的代码。。。希望有人能用一个像我这样的新手程序员比我的老师和我的文章更容易理解的术语来表达 标题: #ifndef CCC_TIME_H #define CCC_T

我正在处理一个在头文件中定义了类(Time)的项目,目标是在我的主函数中使用该类来确定两个时间之间的差异。我已经讨论过这个类,但是我还不能完全理解使用为我定义的类以及在main中使用它的访问器函数的概念

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

标题:

#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;
}    
\ifndef CCC\u TIME\u H
#定义CCC_时间
/**
描述一天中某个时间的类
(在00:00:00和23:59:59之间)
*/
上课时间
{
公众:
/**
构建一天中的某个时间。
@一小时一小时
@参数分钟
@参数秒
*/
时间(整数小时、整数分钟、整数秒);
/**
构造设置为的时间对象
构造函数执行的时间。
*/
时间();
/**
获取此时间的小时数。
@报时
*/
int get_hours()常量;
/**
获取此时间的分钟数。
@返回会议记录
*/
int get_minutes()常量;
/**
获取此时间的秒数。
@返回秒数
*/
int get_seconds()常量;
/**
计算此时间与另一时间之间的秒数。
@另一次
@返回此时间与t之间的秒数
*/
从(时间t)常数开始的整数秒;
/**
将秒数添加到此时间。
@param s要添加的秒数
*/
无效添加_秒(整数秒);
私人:
整数时间单位为秒;
};
#恩迪夫
___________________
使用名称空间std;
int main()
{
int t;
字符串x;

cout好的,如果我理解你的问题,你应该做的是在你的c类的顶部添加一个include,所以对于这个例子,你将添加

#include "cccTime.h" 
(或无论文件名是什么,请使用“”,而不是通常用于包含)


然后你可以调用方法,就像你总是做的那样:你的对象声明在时间上有一些麻烦,我想。我承认C++语法有时会让我感到不安,但是我认为你需要替换<代码>空余时间(int小时,int min,int秒)。使用类似于

Time startTime;
的内容替换
Time\t now();
,并使用类似于
Time stopTime;
的内容替换
Time\t now();


然后执行
int durationInSeconds=stopTime.seconds_from(startTime);
并报告durationInSeconds作为键入所花费的时间。

您不需要知道它是什么时间。只需使用默认构造函数。默认构造函数“构造一个设置为构造函数执行时间的时间对象。”这使得计算某件事情需要多长时间变得非常容易:

  Time start_time;
  do_lots_of_stuff ();
  Time end_time;
现在,您可以使用
end\u time.seconds\u from(start\u time)
来确定从开始到结束所用的时间。

\35;包括
#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;
}
#包括 #包括“ccc_time.h” 使用名称空间std; int main() { //时间开始时间; 字符串x; const string SAMPLE=“敏捷的棕色狐狸跳过了懒狗”;
当然,这可能是有帮助的,可以指定它是什么语言。可以猜测它是C++,但也可以是其他几个。我真的很感激任何帮助,我不指望任何人为我写这个。我真的可以使用一些帮助(例如,我做错了什么,我必须为开始和结束时间创建变量等)吗?你可能应该在你的问题上添加一个家庭作业标签。这应该是做什么的?
无效时间(int-hour,int-min,int-sec);
这似乎是一个时间声明(…)构造函数,您已经从H文件中获得了它。我想您需要构造一个时间变量。是的,我把它放在思考同样的事情时,我最初有时间()但是我不知道我是否应该有参数,或者它们是隐含的?哦,很抱歉,我没有看到,我现在可以看到这是一个基本的实践,有一些人已经在帮助他了。我已经修改了代码,已经看到了我有哪些错误,但是我现在对我的返回值感到困惑。我应该把我的cout放在哪里?在主要方面?不管怎样,我知道想想看,我有一点过度依赖……我声明了一个int变量,然后将durationInSeconds的值赋给它,并在cout语句中使用该变量。