2012-11-11 130 views
0

我與OOP-初學者>我有3名私人成員變量類的日期和應打印日期在2種方式:公共成員函數問題

  1. 2010年12月25日
  2. 月25 2010

下面的代碼它給人的錯誤:

date.obj : error LNK2019: unresolved external symbol "public: __thiscall Date::Date(void)" ([email protected]@[email protected]) referenced in function "public: void __thiscall Date::printDate(void)" ([email protected]@@QAEXXZ) What i am doing wrong? date.h

#include<iostream> 
#include<string> 
#ifndef DATE_H 
#define DATE_H 
class Date 
{ 
private: 
    int day; 
    int month; 
    int year; 
public: 
    Date(); 
    Date(int d, int m, int y) 
    { 
     day=d; 
     month=m; 
     year=y; 
    } 
    int getDay() const {return day;} 
    int getMonth() const {return month;} 
    int getYear() const {return year;} 
    void printDate(void); 
}; 
#endif 

date.cpp

#include"date.h" 
#include<iostream> 
#include<string> 
const int NR=12; 
void Date::printDate() 
{ 
    Date newDate; 
    std::string Months[]={"January","February", "March" , "April", "May", "June", "July", "August", "September", "Octomber", "November", "December"}; 
    int position; 
    std::string month; 
    position=newDate.getMonth(); 
    for(int i=0;i<NR;i++) 
    { 
     if(i==position) 
     { 
      month=Months[i]; 
     } 
    } 
    std::cout<<month<<" "<<newDate.getDay()<<" "<<newDate.getYear()<<std::endl; 
} 

的main.cpp

#include "date.h" 
#include <iostream> 
int main() 
{ 
    int d; 
    int m; 
    int y; 
    std::cout<<"Enter day: "; 
    std::cin>>d; 
    std::cout<<"Enter month: "; 
    std::cin>>m; 
    std::cout<<"Enter years: "; 
    std::cin>>y; 
    Date newDate(d,m,y); 
    std::cout<<newDate.getMonth()<<"/"<<newDate.getDay()<<"/"<<newDate.getYear()<<std::endl; 
    newDate.printDate(); 
} 

回答

4

的錯誤是很清楚的:你宣佈構造爲您Date類,但沒有定義他們cpp文件裏面。

您應該爲這些構造函數添加定義。他們可能只是看起來像這樣:

Date::Date() {} 

也許

Date::Date() { 
    d = 1; 
    m = 1; 
    y = 1970; 
} 

至少不會打印廢話,如果你打電話

Date myDate; 
myDate.printDate(); 

編輯:

由於由Mat建議,你應該在c時使用構造函數初始值設定項列表一個。你等它使用參數的構造函數應該是這樣的一個初始化列表:

Date(int d, int m, int y) : 
    day(d), month(m), year(y) {} 

在你的情況,你的構造函數調用daymonthyear然後分配數值,空構造使用時,而初始化列表,Date構造函數使用參數daymonthyear調用構造函數。

+0

引入初始值設定項列表是個好主意。 – Mat

+0

@Mat仍然有很多人沒有C++ 11(包括我在內!) – alestanis

+0

成員initilizer列表不是新的C++ 11。我的意思是:'Date :: Date():d(1),... {}' – Mat

0

alestanis是正確的,你需要做的改變來擺脫錯誤。但是你的代碼仍然是非常錯誤的。你顯然在面向對象方面掙扎。

在你的printDate方法中,你應該只打印Date類的成員變量。你不應該聲明一個新的變量。像這樣做

void Date::printDate() 
{ 
    std::cout<<Months[m]<<" "<<d<<" "<<y<<std::endl; 
} 

比你寫的代碼簡單得多。

1

嗯,我想你也許在這裏誤解了很多東西。首先,成員數據:在printDate()函數的內部,您可以直接引用日期對象的成員變量。其次,你不需要那麼for循環,你只能說

months[position] 

第三,全球常量不是存儲數組的大小的最佳途徑。如果您需要知道數組的大小,你可以叫

months.size() 

四月份進行陣列可以成員數據,因此您無需在每次打印日期被調用時聲明瞭。最後,您不需要在已包含在頭文件中的cpp文件中包含任何內容。

所以,你的新類應該是這樣的:

Date.h:

#include<iostream> 
#include<string> 
#ifndef DATE_H 
#define DATE_H 
class Date 
{ 
private: 
    int day; 
    int month; 
    int year; 
    const std::string months[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}; 
public: 
    Date(){} 
    Date(int d, int m, int y) 
    int getDay() const {return day;} 
    int getMonth() const {return month;} 
    int getYear() const {return year;} 
    void printDate(void); 
}; 
#endif 

然後Date.cpp:

#include "Date.h" 
Date::Date(int d, int m, int y) 
{ 
    day=d; 
    month=m; 
    year=y; 
} 
void Date::printDate(void) 
{ 
    std::cout<<months[month]<<" "<<day<<" "<<year<<std::endl; 
} 

我知道大多數的這對你似乎毫無意義想要做的事情,但是你可以很快地讓自己陷入C++的困境,因爲它只是讓你做任何你想做的事情,所以最好學習好的編程習慣,並學習語言的力量ri當你開始使用它時。