即使有警卫,也要重新定义重新定义的错误

时间:2012-11-06 06:42:50

标签: c++

我有一个我试图使用的基本文件。

#ifndef POINT_GUARD
#define POINT_GUARD

//------------------------------------------------------------------------------

struct Point {
    int x, y;
    Point(int xx, int yy) : x(xx), y(yy) { }
    Point() :x(0), y(0) { }
};

//------------------------------------------------------------------------------

inline bool operator==(Point a, Point b) { return a.x==b.x && a.y==b.y; } 

//------------------------------------------------------------------------------

inline bool operator!=(Point a, Point b) { return !(a==b); }

//------------------------------------------------------------------------------

#endif // POINT_GUARD

请注意,它被包裹在警卫中。现在将其导入两个不同的文件中。不过我收到了一个错误。

它一旦命中struct Point就会抱怨它是“重新定义点”。有什么想法可以在这里发生吗?

1 个答案:

答案 0 :(得分:1)

我无法使用给定的输入重现错误。我将您的代码放在test.h中,并为test.cpp

写了这个代码
#include "test.h"
#include "test.h"

运行g++ -Wall -c test.cpp不会产生任何错误或警告,并且通过预处理器运行它会显示struct Point仅被声明一次,因此警卫正在工作。

我猜在你引用的代码之外的其他地方有一个同名的声明?