初学者C ++标题/类原型设计错​​误

时间:2014-03-19 00:54:11

标签: c++ prototype header-files

我正在尝试编写一个类来处理以分钟/小时/日/月/年格式编写的日期和时间。

我可以处理逻辑,但我不知道C ++头文件应该如何工作(第一次学习C ++并且真的不喜欢它......)。

我已经编写了一些其他文件作为此程序的一部分,但没有一个头文件包含Date_Time.h。包含Date_Time.h的唯一地方是main.cpp和Date_Time.cpp。

我做错了什么? (希望简单明了......)

我的标题(Date_Time.h)如下所示:

    /* *** Date_Time.h *** */
#ifndef DATE_TIME
#define DATE_TIME

using namespace std;

class Date_Time
{
  private:
  int imot; /* integer minute of time */
  int ihot; /* integer hour   of time */
  int idot; /* integer day    of time */
  int icot; /* integer month  of time (using 'c' for calendar month) */
  int iyot; /* integer year   of time */
  Date_Time() {}

  public:
  Date_Time(string st);     /* constructor */
  void AddMinutes (int im); /* add minutes */
  string ToString();            /* prints time */
};

#endif // DATE_TIME

我的源文件如下所示:

    /* *** Date_Time *** */
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "Date_Time.h"
#include "Tools.h"
using namespace std;

Date_Time::Date_Time (string st)
{
  vector<string> sp;
  string sdl = "/";
  Tools::Parse(st, sp, sdl);

  this->imot = Tools::StoI(sp[5]);
  this->ihot = Tools::StoI(sp[4]);
  this->idot = Tools::StoI(sp[3]);
  this->icot = Tools::StoI(sp[2]);
  this->iyot = Tools::StoI(sp[1]);
}

Date_Time::AddMinutes(int im)
{
  int idpm;

  imot += im;
  ihot += imot/60, imot = imot%60;
  idot += ihot/24, ihot = ihot%24;

  switch (imot)
  {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12: idpm = 31;
    break;
  case 4:
  case 6:
  case 9:
  case 11: idpm = 30;
    break;
  case 2: idpm = 28;
    break;
  default:
    break;
  }

  if (iyot%4 == 0 && (iyot%100 == 0 && iyot%400 == 0) && imot == 2)
    idpm++;

  icot += idot/idpm, idot = idot%idpm;
  iyot += icot/12  , icot = icot%12;
}

Date_Time::ToString()
{
  string sout;
  stringstream ss;
  ss << imo4 << "/" << ihot << "/" << idot << "/" << icot << "/" << iyot;
  return ss.str();
}

我得到的错误如下:

Date_Time.cpp:23:29: error: ISO C++ forbids declaration of 'AddMinutes' with no type [-fpermissive]
Date_Time.cpp:23:1: error: prototype for 'int Date_Time::AddMinutes(int)' does not match any in class 'Date_Time'
In file included from Date_Time.cpp:6:0:
Date_Time.h:19:8: error: candidate is: void Date_Time::AddMinutes(int)
Date_Time.cpp:59:21: error: ISO C++ forbids declaration of 'ToString' with no type [-fpermissive]
Date_Time.cpp:59:1: error: prototype for 'int Date_Time::ToString()' does not match any in class 'Date_Time'
In file included from Date_Time.cpp:6:0:
Date_Time.h:20:10: error: candidate is: std::string Date_Time::ToString()
Process terminated with status 1 (0 minutes, 0 seconds)
6 errors, 0 warnings (0 minutes, 0 seconds)

1 个答案:

答案 0 :(得分:2)

在您的cpp中,您缺少AddMinutes的返回类型:

void Date_Time::AddMinutes(int im)
^^^^
{
...

和ToString

string Date_Time::ToString()
^^^^^^

讨厌说出来,但编译错误很明显。