2012-02-22 60 views
1

我有一個文件GetL.hxx爲頭創建的源文件文件

#ifndef GetL_included 
#define GetL_included 
#include <iostream> 
using namespace std; 
class GetL 
{ 
public: 
    virtual int getWidth(); 
}; 
#endif //GetL_include 

這裏的類GetL只包含一個虛函數。我應該把在源文件即GetL.cxx

+0

你應該在末尾有'#endif //!GetL_included'。 – 2012-02-22 09:27:05

+0

@AdeYU你能解釋一下這個區別嗎 – 2012-02-22 09:32:39

+0

可能是[用C++創建hxx文件的cxx文件]的副本(http://stackoverflow.com/questions/9390953/create-cxx-file-for-hxx-file-in- c) – 2012-02-22 09:49:23

回答

3
#include "GetL.hxx" 

int GetL::getWidth() { 
    // your code goes here 
} 

順便說一句,在頭文件is not a good practiceusing namespace std;

+0

我怎麼可以'#include '沒有'使用命名空間std' ?? – 2012-02-22 09:24:00

+0

我想創建一個虛擬方法,以便可以繼承GetL類並覆蓋getWidth方法。如果我給出這樣的實現,它會擊敗我創建它的原因。 – 2012-02-22 09:26:54

+0

如果#include 沒有使用名稱空間標準,你可以在標準名稱空間中引用iostream的內容。例如,你使用'std :: cout',而不僅僅是'cout'。這可以避免污染全局名稱空間。儘管如此,你並沒有使用iostream頭文件中的任何東西,所以你不需要包含它。 – 2012-02-22 09:42:41