为头创建的源文件文件

问题描述:

我有一个文件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://*.com/questions/9390953/create-cxx-file-for-hxx-file-in- c) – 2012-02-22 09:49:23

#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