0

我有一个我正在尝试使用的基本文件。

#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它是“点的重新定义”。有什么想法可以在这里发生吗?

4

1 回答 1

1

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

#include "test.h"
#include "test.h"

运行g++ -Wall -c test.cpp不会产生错误或警告,并且通过预处理器运行它表明struct Point它只声明了一次,因此防护正在工作。

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

于 2012-11-06T06:50:41.890 回答