好了,我们在我的C++类覆盖类,并在最后的演讲中,我们了解到,如果你#包括同一类在同一个文件多次不好的事情可能发生,而防止这种情况的方法是在头文件中使用#ifdef,#define和#endif。所以这里只是一个简单的程序,我试图写,因为这个家伙#,它失败了。看起来像#ifdef之后的代码被一个compailer忽略了。这里有什么问题 ?C++类,在reguards混乱支持#ifdef,#定义和#endif
这里是程序,它由3个文件,也请注意,我知道,如果没有在.h文件中程序#东西正常工作,并在这个特定的程序,我不甚至需要他们。我正在开发一个需要使用它们的大项目,而他们只是不工作。
谢谢。
==========主文件===========
#include "Circle.h"
int main()
{
Circle C(5);
C.output();
return 0;
}
======== Circle.h文件==== ======
#ifdef CIRCLE_H
#define CIRCLE_H
#include <iostream>
using namespace std;
class Circle
{
public:
Circle(int);
void output();
private:
int n;
};
#endif
======== Circle.cpp文件========
#include "Circle.h"
Circle::Circle(int numb)
{
n=numb;
}
void Circle::output()
{
cout<<"The number is "<<n<<endl;
}
你想'#ifndef':如果宏是*尚未确定*然后继续。使用'#ifdef',它将永远不会成功。 – rici 2015-04-04 21:13:33
是的,在你的代码中,只有已经创建了'include guard'才会被创建。这就是为什么你需要使用'#ifndef'而不是'#ifdef'。 – wowofbob 2015-04-04 21:19:37
另外,请在最后一个'#include'之后将''namespace std;'从所有头文件移动到源文件。这就是它应该如何使用的。在标题中总是使用全名('std :: string'而不是'string')。 – Dialecticus 2015-04-04 21:20:56