2014-03-19 34 views
-1

我想寫一個類來處理格式爲分/小時/日/月/年的日期和時間。初學C++頭/類原型錯誤

我可以處理邏輯,但我不知道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) 

回答

2

在你的CPP你缺少返回類型的AddMinutes:

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

和toString

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

討厭這樣說,但編譯器錯誤是一種明白無誤的。